hi guys can someone help me with this problem below I am using qt5.9.1 on win7
I got the error when I was trying to compile a example source code.
error: undefined reference to `MyClass::MyClass(QString const&, QObject*)'
hi guys can someone help me with this problem below I am using qt5.9.1 on win7
I got the error when I was trying to compile a example source code.
error: undefined reference to `MyClass::MyClass(QString const&, QObject*)'
Where is MyClass::MyClass(QString const&, QObject*) defined?
You should have no trouble finding your answer here: error: undefined reference to
thank you for the response it is defined in header file and the constructor implemented in .cpp source file.
And have you included that cpp file in the list of files that gets compiled (and more importantly) linked into the executable file produced by your project?it is defined in header file and the constructor implemented in .cpp source file.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Just to add to d_stranz's answer, you usually only declare the function in the header file and give the definition in the source file. I am fairly certain that, while the declaration does not need it, the definition needs a variable name. for example:
.h
.cppQt Code:
To copy to clipboard, switch view to plain text mode
At least that is the way I learned to do it.Qt Code:
To copy to clipboard, switch view to plain text mode
Strictly speaking, the definition needs a named argument only if the argument is used in the body of the function. For example this code is perfectly fine:the definition needs a variable name
Qt Code:
// .cpp void MyClass::someFunction( int ) { // do something not involving the int argument }To copy to clipboard, switch view to plain text mode
You might encounter this when you override a virtual function defined in a base class but your override doesn't use the argument.
This is not the cause of the OP's linking problem. The "undefined reference" error message that is displayed by most linkers does not usually include the names of the arguments, just the method's signature (as he showed). His error is due to not telling the linker that the binary (.o or .obj) file produced from compiling the .cpp file should be linked in to the executable.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
I have run into that more than once.
Although not necessary, I usually give a name in the declaration, just for consistency, and to make it obvious.
I did not realize that. It is also not the first place I would look either. I just thought I would mention it, as it might be a possibility.
Sure, in the declaration is fine, especially if it is in code you might leave for a while and then go back and scratch your head trying to remember "what's that argument supposed to be?"Although not necessary, I usually give a name in the declaration, just for consistency, and to make it obvious.
In the function definition however, some compilers at higher warning levels will complain about "unused arguments" if you add the name there but don't use it. That is often why you see in code things like this:
Qt Code:
void MyClass::someFunction( int /* foo */ ) { } // or void MyClass::someFunction( int foo ) { UNUSED( foo ) }To copy to clipboard, switch view to plain text mode
where the UNUSED() macro just inserts a "do nothing" reference to keep the compiler happy and to make it clear to a reader that the argument really isn't used.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks