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()<< " "
<<getTreatArra开发者_运维百科y().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.h
in Animal.cpp
.
EDIT: To expand on the reason, the error message translates to:
"I see that you've declared a class called Treatment. But I don't see its implementation!".
So to let the compiler see its implementation where you're accessing the members of the class Treatment
, you need to #include Treatment.h
in your Animal.cpp
. I see the reason why you don't want it to be in Animal.h
, because Animal.h
is being included in Treatment.h
, which could cause compiler to get into a problem like:
"Okay! I'm parsing Animal.cpp...
- It includes Animal.h...
-- I'm going to expand Animal.h...
-- Animal.h includes Treatment.h...
--- I'm going to expand Treatment.h...
--- Treatment.h includes Animal.h...
---- I'm going to expand Animal.h....
---- (Loops)"
This kind of gets avoided by the #pragma once
or #ifdef
guards. But when them come into action, compiler goes like this:
"Okay! I'm parsing Animal.cpp...
- It includes Animal.h...
-- I'm going to expand Animal.h...
-- Animal.h includes Treatment.h...
--- I'm going to expand Treatment.h...
--- Treatment.h is using class called Animal...
--- WHERE IS THE DEFINITION FOR ANIMAL?.... ERROR!
--- (The compiler did not come to the point where class Animal was defined!)
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.h
that "Keep an eye out for a class called Treatment", as long as it's not being used in Animal.h
and the use of Treatment class is as a pointer, the compiler will not complain with the header file side of Animal class. But in Animal.cpp
you're calling a function from Treatment class. Then the compiler goes:
Hey! You told me to look out for a class called Treatment. And now you're asking me to resolve that function definition, but I don't see the implementation of Treatment!
And hence the reason to include Treatment.h
in your Animal.cpp
. Animal.cpp will get compiled independently of Treatment.cpp and vice-versa and hence should not cause the clash I mentioned above.
Since you used a forward declaration in the header, you still need to include the Treatment.h file in your animal.cpp
Otherwise, the compiler still does not know what the Treatment class is.
精彩评论