Results 1 to 8 of 8

Thread: undefine reference error

  1. #1
    Join Date
    May 2016
    Posts
    10
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default undefine reference error

    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*)'

  2. #2
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    Where is MyClass::MyClass(QString const&, QObject*) defined?

    You should have no trouble finding your answer here: error: undefined reference to

  3. #3
    Join Date
    May 2016
    Posts
    10
    Thanks
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: undefine reference error

    thank you for the response it is defined in header file and the constructor implemented in .cpp source file.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    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?
    <=== 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.

  5. #5
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    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
    Qt Code:
    1. MyClass(QString const&, QObject*)
    To copy to clipboard, switch view to plain text mode 
    .cpp
    Qt Code:
    1. MyClass::MyClass(QString const& string, QObject* object)
    To copy to clipboard, switch view to plain text mode 
    At least that is the way I learned to do it.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    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:

    Qt Code:
    1. // .cpp
    2.  
    3. void MyClass::someFunction( int )
    4. {
    5. // do something not involving the int argument
    6. }
    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.

  7. #7
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    Quote Originally Posted by d_stranz View Post
    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.

    Quote Originally Posted by d_stranz View Post
    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.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: undefine reference error

    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:

    Qt Code:
    1. void MyClass::someFunction( int /* foo */ )
    2. {
    3. }
    4.  
    5. // or
    6. void MyClass::someFunction( int foo )
    7. {
    8. UNUSED( foo )
    9. }
    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.

Similar Threads

  1. error: undefined reference to. Where?
    By remizero in forum Installation and Deployment
    Replies: 4
    Last Post: 8th April 2017, 18:06
  2. undefined reference to vtable error
    By PaulDaviesC in forum Newbie
    Replies: 4
    Last Post: 20th April 2012, 16:11
  3. Getting Undefined Reference Error
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2009, 02:12
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  5. Undefined Reference error!!!
    By Kapil in forum Newbie
    Replies: 25
    Last Post: 28th March 2006, 12:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.