Results 1 to 8 of 8

Thread: Problem with class and definitions

  1. #1
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with class and definitions

    Hi there i have a problem with a class.

    So what i did:
    I created a incident.h with my class "IncidentLocation".

    Qt Code:
    1. class IncidentLocation
    2. {
    3. public:
    4. IncidentLocation(QString city, QString street, int houseno, QString floor, QString company, QString building);
    5. QString getCity();
    6. QString getStreet();
    7. int getHouseno();
    8. QString getFloor();
    9. QString getCompany();
    10. QString getBuilding();
    11.  
    12. private:
    13. QString city;
    14. QString street;
    15. int houseno;
    16. QString floor;
    17. QString company;
    18. QString building;
    19. };
    To copy to clipboard, switch view to plain text mode 

    So after that i created "incident.cpp" to define all functions of the class:

    Qt Code:
    1. #include "incident.h"
    2.  
    3. IncidentLocation::IncidentLocation(QString city, QString street, int houseno, QString floor, QString company, QString building)
    4. :city(city), street(street), houseno(houseno), floor(floor), company(company), building(building){}
    5.  
    6. IncidentLocation::getCity()
    7. {
    8. return city;
    9. }
    10.  
    11. IncidentLocation::getStreet()
    12. {
    13. return street;
    14. }
    15.  
    16. IncidentLocation::getHouseno()
    17. {
    18. return houseno;
    19. }
    20.  
    21. IncidentLocation::getFloor()
    22. {
    23. return floor;
    24. }
    25.  
    26. IncidentLocation::getCompany()
    27. {
    28. return company;
    29. }
    30.  
    31. IncidentLocation::getBuilding()
    32. {
    33. return building;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Now QtCreator says for every get-Function:
    >> ISO C++ forbids declaration of 'getXXXX' with no type
    >> prototype for 'int IncidentLocation::getXXXX()' does not match any in class 'IncidentLocation'
    >> candidate is: QString IncidentLocation::getXXXX()


    What the fuck is the problem? Dont get the error i made.
    Please help not to went crazy -.-

    Greetz

  2. #2
    Join Date
    Jan 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Problem with class and definitions

    You have to add the return type to your implementation:
    Qt Code:
    1. QString IncidentLocation::getCity()
    2. {
    3. return city;
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with class and definitions

    ok i did that before.
    but if i add a return type following happens:

    error.jpg

    my main.cpp looks like this:

    Qt Code:
    1. #include <incident.cpp>
    2. #include <QApplication>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. IncidentLocation iloc("Remagen", "Oberdorfstraße", 3, "1", "TorbenCom", "Unkelbacher State Building");
    10.  
    11. qDebug() << iloc.getBuilding() << "\n";
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Problem with class and definitions

    Yes, you have to include the "incident.h" to your main.cpp - and not the "incident.cpp" file! This means, that you create an object of your main.cpp (which includes the incident.cpp) and create another object of incident.cpp. The linker finds now each method twice, and gives you an linker error.

    When you remove the return type, you get a "compiler error" which appears before the linking - the linking isn't done, since the compiler finished with an error.

  5. #5
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with class and definitions

    omg -.-
    such an easy solution thanks. i am never made classes in external header files so i didn't know this^^

    but it works now

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with class and definitions

    why is this thread in the qt area?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with class and definitions

    aww shit yeah you're right amleto.
    i was just working with QtCreator and didn't think about the fact that this is a general C++ question and not related to Qt^^

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with class and definitions

    never mind. maybe a mod can move it.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 4
    Last Post: 30th March 2011, 07:55
  2. Replies: 7
    Last Post: 2nd September 2010, 19:42
  3. Conflicting header definitions
    By jacky in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2009, 09:51
  4. QDomDocument and xml version/encoding definitions
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 18th September 2008, 14:34
  5. const member definitions in a class ....
    By TheKedge in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 13: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.