PDA

View Full Version : Problem with class and definitions



Terreox
21st January 2012, 16:49
Hi there i have a problem with a class.

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


class IncidentLocation
{
public:
IncidentLocation(QString city, QString street, int houseno, QString floor, QString company, QString building);
QString getCity();
QString getStreet();
int getHouseno();
QString getFloor();
QString getCompany();
QString getBuilding();

private:
QString city;
QString street;
int houseno;
QString floor;
QString company;
QString building;
};

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


#include "incident.h"

IncidentLocation::IncidentLocation(QString city, QString street, int houseno, QString floor, QString company, QString building)
:city(city), street(street), houseno(houseno), floor(floor), company(company), building(building){}

IncidentLocation::getCity()
{
return city;
}

IncidentLocation::getStreet()
{
return street;
}

IncidentLocation::getHouseno()
{
return houseno;
}

IncidentLocation::getFloor()
{
return floor;
}

IncidentLocation::getCompany()
{
return company;
}

IncidentLocation::getBuilding()
{
return building;
}


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

blueSpirit
21st January 2012, 16:53
You have to add the return type to your implementation:

QString IncidentLocation::getCity()
{
return city;
}

Terreox
21st January 2012, 17:08
ok i did that before.
but if i add a return type following happens:

7297

my main.cpp looks like this:


#include <incident.cpp>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

IncidentLocation iloc("Remagen", "Oberdorfstraße", 3, "1", "TorbenCom", "Unkelbacher State Building");

qDebug() << iloc.getBuilding() << "\n";

return app.exec();
}

blueSpirit
21st January 2012, 17:23
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.

Terreox
21st January 2012, 18:11
omg -.-
such an easy solution :D thanks. i am never made classes in external header files so i didn't know this^^

but it works now :D

amleto
21st January 2012, 18:19
why is this thread in the qt area?

Terreox
21st January 2012, 20:18
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^^

amleto
22nd January 2012, 13:39
never mind. maybe a mod can move it.