Possible Duplicate:
Is this possible in C++? toString(ClassName* class)
I’m trying to use toString but I have a problem in there.
I ask this question second time, because the first time I asked before had insufficient information. I just worried about the length of question. Sorry about that.
toString is in the Animal class, and Animal class has vector<Treatment> treatArray; as a data member, but the thing is I can use the data member itself, but I cannot get the data member of treatArray. These are my code:
Animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include "jdate.h"
//#include "Treatment.h"
#include <vector>
#include <sstream>
class Treatment;
class Animal{
protected:
int id;
double weight;
int yy;
int mm;
int dd;
double dose;
double accDose;
char sex;
vector<Treatment*> treatArray;
public:
Animal();
Animal(int newid, double newweight, int yy, int mm, int dd, char newsex, vector<Treatment*> treatArray);
~Animal();
void setTreatArray(vector<Treatment*> treatArray);
vector<Treatment*> getTreatArray();
string toString();
};
Treatment.h
#ifndef TREATMENT_H
#define TREATMENT_H
#include "jdate.h"
class Treatment{
private:
int id;
jdate dayTreated;
double dose;
double accDose;
public:
Treatment(int id,jdate dayTreated, double dose);
Treatment();
~Treatment();
};
#endif
Animap.cpp
#include "Animal.h"
//using namespace std;
Animal::Animal(int newid, double newweight, int yy, int mm, int dd, char newsex, vector<Treatment*> treatArray)
{
id = newid;
weight = newweight;
yy = yy;
mm = mm;
dd = dd;
dose = 0;
accDose = 0;
sex = newsex;
}
Animal::Animal()
{
id = 0;
weight = 0;
yy = 0;
mm = 0;
dd = 0;
dose = 0;
accDose = 0;
sex = ' ';
}
void Animal::setTreatArray(vector<Treatment*> treatArray){treatArray = treatArray;}
vector<Treatment*> Animal::getTreatArray(){return treatArray;}
string Animal::toString()
{
jdate DOB(getYY(),getMM(),getDD());
ostringstream ostr;
ostr<<"Cattle / Sheep: "<<getSex()<<", Weight: "<<getWeight()
<<" kg. DOB: " <<DOB.toString()<<" Accum Dose " <<getAccDose() << "mg" << endl;
if(getTreatArray().size()==0)
ostr<<"\n No History Found\n";
else
{
for(int i=0;i<getTreatArray().size();i++)
{
//UNTIL HERE, NO ERROR FOUND, BUT ERROR OCCURS FROM THE STATEMENT BELOW
ostr<<" Treatment: " << getTreatArray().at(i)->getID() << " "
<<getTreatArray().at(i)->getDayTreated().toString()<< " "
<<getTreatArray().at(i)->getDose() <<"mg\n";
}
}
return ostr.str();
}
There are setter and getter for each class, and I cut it down.
Also, I thought it’s because of initalisation of the vector, but I googled regarding initalising vector, and it says that vector is automatically initialised, so I don’t have to initailise manually. Now I don’t know what the problem is 🙁
The error message is:
1 IntelliSense: pointer to incomplete class type is not allowed l:\2011-08\c++\assignment\drug management\drug management\animal.cpp 97 30 Drug Management
You should include
Treatment.hinAnimal.cpp.EDIT: To expand on the reason, the error message translates to:
So to let the compiler see its implementation where you’re accessing the members of the class
Treatment, you need to#include Treatment.hin yourAnimal.cpp. I see the reason why you don’t want it to be inAnimal.h, becauseAnimal.his being included inTreatment.h, which could cause compiler to get into a problem like:This kind of gets avoided by the
#pragma onceor#ifdefguards. But when them come into action, compiler goes like this:It will also depend on whether the compiler started with Treatment.cpp or Animal.cpp. If it was Treatment.cpp, it would complain about missing definition of Treatment (just the opposite scenario of what’s happened with Animal).
Now that you’ve declared to the compiler in
Animal.hthat “Keep an eye out for a class called Treatment”, as long as it’s not being used inAnimal.hand the use of Treatment class is as a pointer, the compiler will not complain with the header file side of Animal class. But inAnimal.cppyou’re calling a function from Treatment class. Then the compiler goes:And hence the reason to include
Treatment.hin yourAnimal.cpp. Animal.cpp will get compiled independently of Treatment.cpp and vice-versa and hence should not cause the clash I mentioned above.