QCommandLineParser inheritance and vector problem.
-
@Khamza said in QCommandLineParser inheritance and vector problem.:
QCommandLineOption *option = new QCommandLineOption("Option");
Any reason you're allocating on the heap?
Please show how you're calling the base constructor.
I see no reason why the type of options member would cause this error. Try to rename options - not sure there is a property with this name in QCommandLineParser. -
class CommandLineParser : public QCommandLineParser { public: CommandLineParser() : QCommandLineParser() { QCommandLineOption *option = new QCommandLineOption("Option"); addOption(*option); } private: QVector<QCommandLineOption *> options; }; int main(int argc, char** argv) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
Use heap allocation to freely use option in class without many copyings.
Besides can it be IDE mistake?
P.S renaming option also didn't help... -
@Khamza said in QCommandLineParser inheritance and vector problem.:
Besides can it be IDE mistake?
Where are you getting the
error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'
from? You say "This gives me warning but compiles and works fine", so I don't really understand. You get this at runtime, or what?
FWIW: At Qt6
QVector
is gone, it just becomesQList
. If you say your code works or doesn't warn withQList
instead ofQVector
you could replace and pretend you never saw this issue. :) -
@Khamza Please post code as text, not pictures.
This comes from code model I guess. Does your project compile?"Use heap allocation to freely use option in class without many copyings" - why do you think there is less copying if you allocate it on the heap? You are passing it by reference, not pointer.
-
@jsulm
Thisclass CommandLineParser : public QCommandLineParser { public: CommandLineParser(QApplication &a) : QCommandLineParser() { QCommandLineOption option = QCommandLineOption("option", "this is description", "argument"); addHelpOption(); // main.cpp:38:9: error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser' addVersionOption(); // same here addOption(option); // same here process(a); qInfo() << "Names: " << optionNames(); // same here qInfo() << "Is set:" << isSet(option) << value(option); // also here } public: QVector<QCommandLineOption *> options; // QList<QCommandLineOption *> options; }; int main(int argc, char** argv) { QApplication a(argc, argv); MainWindow w; CommandLineParser parser(a); w.show(); return a.exec(); }
gives me
Names: ("option") Is set: true "asd"
So, as you see works fine. You say it's false positive, but may it be UB?
-
@Khamza said in QCommandLineParser inheritance and vector problem.:
but may it be UB?
Do you mean undefined behavior? Why should it be?
As I said: this error comes from code model, not from the compiler when you compile your project. Code model can generate false positive errors/warnings. Important is what the compiler says when you compile. -
@jsulm
Ok, got it. Thanks a lot.
P.S
So, i thought that that i need my options must "live" when use parser.process(). But internallyQLineOptionParser
copies all option toQList<QCommandLineOption> commandLineOptionList;
so i simply remove that vector... -
-
Hi,
@Khamza said in QCommandLineParser inheritance and vector problem.:
class CommandLineParser : public QCommandLineParser { public: CommandLineParser() : QCommandLineParser() { QCommandLineOption *option = new QCommandLineOption("Option"); addOption(*option); }
One small side note: there's no need to allocate the option on the heap. You are just adding a layer of indirection for no benefit.
-
@Khamza
Just to wrap up.-
I think you are going to get rid of the
new
, so it does not matter now, but be aware that the way you wrote will leak the createdQCommandLineOption
. You assign to local variable and do notdelete
it. It would show up if you used e.g.valgrind
. -
The message you get is only from the code model, and seems inexplicable. Which model are you using? If it's the internal one that no longer gets any attention and might be "off". You might wish to check if you are using the clang code model, it's settable somewhere in settings.
-
I'm not sure whether the red lines are from your editing or from the code model. If the latter it looks like it's got itself lost all over the place.
-
If I understand you rightly, you said the "cannot initialize ..." message only occurs if you have
QVector<QCommandLineOption *>
and not if you make that aQList
. That line comes after the error message one and just has no relationship to it, so it's a mystery. -
As I wrote earlier, why don't you change over from
QVector
toQList
anyway if that solves it, as that will be better going forward and makes no difference to you now?
-
-
@JonB
Thanks for your response.- +
- I use built-in model. Yes it's clang
- I thought it's because of git.
- +
- So, i thought that
QVector
is more preferable thanQList
. Also i already usedQVector
in all other code.
P.S. Why should i call explicitlyQCommandLineParser
inCommandLineParser
? Isn't it called implicitly?