这是可能在C ++吗? toString(ClassName * class) [英] Is this possible in C++? toString(ClassName* class)
问题描述
我想使用带有类参数的toString,但由于某种原因有一个错误。
代码是:
I'd like to use toString with class argument, but for some reason there is an error. The code is:
Animal.h
#include "Treatment.h"
#include "jdate.h"
#include <vector>
class Animal{
protected:
int id;
double weight;
int yy;
int mm;
int dd;
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();
};
Treatment.h
#ifndef TRE_H
#define TRE_H
#include <string>
#include <sstream>
#include "jdate.h"
#include "Animal.h"
#include "Cattle.h"
#include "Sheep.h"
class Treatment{
private:
int id;
jdate dayTreated;
double dose;
public:
Treatment(int id,jdate dayTreated, double dose);
Treatment();
~Treatment();
string toString(Animal* a);
};
#endif
Treatment.cpp b
$ b
Treatment.cpp
#include "Treatment.h"
using namespace std;
Treatment::Treatment(int newid,jdate newdayTreated, double newdose){
id=newid;
dayTreated = newdayTreated;
dose = newdose;
}
Treatment::Treatment(){
id=0;
dose=0;
}
Treatment::~Treatment(){}
string Treatment::toString(Animal* a)
{
string aa;
return aa;
}
toString在Treatment类中。我不知道,但我认为这是因为动物有
矢量treatArray。它实际上是重要吗?对不起,我不能把错误消息在这里,因为一旦我声明toString,由于某些原因吨错误发生,如
toString is in Treatment class. I'm not sure but I think it's because Animal has vector treatArray;. Does it actually matter? Sorry that I cannot put the error messages here, because once I declare toString, for some reason tons of errors occur, such as
Error 1 error C2065: 'Treatment' : undeclared identifier l:\2011-08\c++\assignment\drug management\drug management\animal.h 30 1 Drug Management
推荐答案
// Animal.h
// #include "Treatment.h" remove this
class Treatmemt; // forward declaration
class Animal
{
...
};
在您的版本中,Treatment.h和Animal.h包含彼此。你需要使用forward声明来解决这个循环依赖。在.cpp文件中,包括所有必要的h文件。
In your version, Treatment.h and Animal.h include each other. You need to resolve this circular dependency using forward declaration. In .cpp files, include all necessary h-files.
这篇关于这是可能在C ++吗? toString(ClassName * class)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!