PDA

View Full Version : undefine reference error



gotronics
19th July 2017, 23:42
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*)'

admkrk
20th July 2017, 08:47
Where is MyClass::MyClass(QString const&, QObject*) defined?

You should have no trouble finding your answer here: error: undefined reference to (https://www.google.com/search?q=error%3A+undefined+reference&ie=utf-8&oe=utf-8)

gotronics
20th July 2017, 16:17
thank you for the response it is defined in header file and the constructor implemented in .cpp source file.

d_stranz
20th July 2017, 17:48
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?

admkrk
21st July 2017, 08:16
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

MyClass(QString const&, QObject*)
.cpp

MyClass::MyClass(QString const& string, QObject* object)
At least that is the way I learned to do it.

d_stranz
22nd July 2017, 21:59
the definition needs a variable name

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:



// .cpp

void MyClass::someFunction( int )
{
// do something not involving the int argument
}


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.

admkrk
23rd July 2017, 06:44
You might encounter this when you override a virtual function defined in a base class but your override doesn't use the argument.
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.


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).
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.

d_stranz
23rd July 2017, 17:19
Although not necessary, I usually give a name in the declaration, just for consistency, and to make it obvious.

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?"

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:



void MyClass::someFunction( int /* foo */ )
{
}

// or
void MyClass::someFunction( int foo )
{
UNUSED( foo )
}


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.