Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: Function previously defined in created file?

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function previously defined in created file?

    Quote Originally Posted by Twey
    But the files were headers.
    Also, yop, when the second button in your code (b2) is connected to the moused() slot, nothing happens.
    Sorry, I've confused this thread with another one.

    The problem is that you have placed code in header files. You can only put declarations, templates and inline functions there (unless you want linking errors). Compiler doesn't have to know anything about .h files --- preprocessor creates a single .cpp file with all headers inside it and then it feeds such file to compiler.

    In this case compiler put code of those methods in both moc_qtrackingbutton.o and app4lin.o, because both moc_qtrackingbutton.cpp and app4lin.cpp include qtrackingbutton.h.

  2. #22
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function previously defined in created file?

    Quote Originally Posted by yop
    Don't you get a vtable blah blah error in that case?
    No, it should prevent it. I was thinking about a bit different situation --- when you place class definition in .cpp file and it has Q_OBJECT macro inside it, qmake won't notice that and you will have a "undefined reference to vtable" error, like here.

  3. #23
    Join Date
    Jan 2006
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Function previously defined in created file?

    Quote Originally Posted by yop
    I only tried to compile not add any functionality
    I beg your pardon: I missed a statement.
    Quote Originally Posted by jacek
    In this case compiler put code of those methods in both moc_qtrackingbutton.o and app4lin.o, because both moc_qtrackingbutton.cpp and app4lin.cpp include qtrackingbutton.h.
    So one can't include custom headers containing Q_OBJECT in a header. OK.
    Last edited by Twey; 14th January 2006 at 22:06.

  4. #24
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function previously defined in created file?

    Quote Originally Posted by jacek
    No, it should prevent it. I was thinking about a bit different situation --- when you place class definition in .cpp file and it has Q_OBJECT macro inside it, qmake won't notice that and you will have a "undefined reference to vtable" error, like here.
    Yeah it's what I meant that #include foo.moc handles the "vtable blah blah" errors but that was not the case. The solution was clear as you posted above while I was thinking of... well I don't really know what I was thinking I got a bit confused

  5. #25
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function previously defined in created file?

    Quote Originally Posted by Twey
    So one can't include custom headers containing Q_OBJECT in a header. OK.
    No, you can do this. The problem you have encountered has nothing to do with Q_OBJECT.

    Suppose you have such project:
    Qt Code:
    1. // a.h
    2. #ifndef __A_H__
    3. #define __A_H__
    4.  
    5. class A
    6. {
    7. public:
    8. virtual int foo();
    9. };
    10.  
    11. int A::foo() { return 10; }
    12.  
    13. #endif
    14.  
    15. // b.h
    16.  
    17. #ifndef __B_H__
    18. #define __B_H__
    19.  
    20. #include "a.h"
    21.  
    22. class B : public A
    23. {
    24. public:
    25. int foo();
    26. };
    27.  
    28. #endif
    29.  
    30. // b.cpp
    31.  
    32. #include "b.h"
    33.  
    34. int B::foo() { return 20; }
    35.  
    36. // main.cpp
    37.  
    38. #include <iostream>
    39. #include "a.h"
    40. #include "b.h"
    41.  
    42. int main()
    43. {
    44. A a;
    45. B b;
    46. std::cout << a.foo() << b.foo() << std::endl;
    47. }
    To copy to clipboard, switch view to plain text mode 
    As you see there are two .cpp files and to obtain the executable you must compile each of those files and link them toghether.

    First those files will be go through preprocessor that will turn them to something like this:
    Qt Code:
    1. // main.cpp
    2. <code from iostream header>
    3.  
    4. class A
    5. {
    6. public:
    7. virtual int foo();
    8. };
    9.  
    10. int A::foo() { return 10; }
    11.  
    12. class B : public A
    13. {
    14. public:
    15. virtual int foo();
    16. };
    17.  
    18. int main()
    19. {
    20. A a;
    21. B b;
    22. std::cout << a.foo() << b.foo() << std::endl;
    23. }
    24.  
    25. // b.cpp
    26.  
    27. class A
    28. {
    29. public:
    30. virtual int foo();
    31. };
    32.  
    33. int A::foo() { return 10; }
    34.  
    35. class B : public A
    36. {
    37. public:
    38. virtual int foo();
    39. };
    40.  
    41. int B::foo() { return 20; }
    To copy to clipboard, switch view to plain text mode 

    As you can see both of these files contain the implementation of the A::foo() method. So after compilation both main.o and b.o files will have the code for that method and linker will raise an error as it won't be able to determine which one is valid.

    b.o(.text+0x0): In function `A::foo()':
    b.cpp: multiple definition of `A::foo()'
    main.o(.text+0x0):main.cpp: first defined here
    collect2: ld returned 1 exit status
    To solve the problem you should either make A::foo() an inline method or move it to a.cpp file.

  6. #26
    Join Date
    Jan 2006
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Function previously defined in created file?

    Ah, I see. OK.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. Regading Driver to connect Postgresql Database
    By dummystories in forum Installation and Deployment
    Replies: 38
    Last Post: 12th March 2009, 08:19
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  4. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13: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.