Results 1 to 6 of 6

Thread: problem with virtual function in a .h

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default problem with virtual function in a .h

    Hello,
    I attach you the class; It works now but I want understand where exactly is the origin of the problem...
    If I do the same thing inside a .cpp, the "inline" keyword isn't necessary; then, I don't understand the compiler message (.net) ("already defined in main.obj")
    Qt Code:
    1. error LNK2005: "public: virtual struct Tree::Node * __thiscall Tree::getRoot(void)const " (?getRoot@Tree@@UBEPAUNode@1@XZ) already defined in main.obj
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Attached Files Attached Files
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with virtual function in a .h

    If you omit the inline keyword and include the header file in more than one other files that get compiled, you'll get two or more definitions of the same function and that is forbidden. Either make the function inline or put it into a .cpp file.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with virtual function in a .h

    I still don't understand; maybe could you me get some page on thinking in c++ where read it?
    I have #include "tree.h" in main.cpp and in tree.cpp; but in .h, I used the keyword "#ifndef ..., #define....., #endif ". Shoulnd't be this enough to prevent that error?
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with virtual function in a .h

    No, because defines are only relevant inside current compilation unit thus define guardians are not enough.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with virtual function in a .h

    Quote Originally Posted by wysota View Post
    No, because defines are only relevant inside current compilation unit thus define guardians are not enough.
    But in my case, isn't "the unit compilation unit" the .h file????
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with virtual function in a .h

    No. header files are included into compilation units. A single compilation unit is a cpp with all included files. You get object files out of implementation files not header files. In your case the definition of the getRoot() method is present in two separate objects and when the linker links them together, it gets confused. Simply move the body of the method to some cpp file that includes your header file and everything will be fine. Just be sure to leave the declaration of the method in the header or you won't be able to use it from within other units.

  7. The following user says thank you to wysota for this useful post:

    mickey (19th April 2008)

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.