我想将toString与类参数一起使用,但是由于某些原因,出现错误。
代码是:

动物

#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();
};


处理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

#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,由于某种原因就会发生大量错误,例如

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”删除此

Treatmemt类; //转发声明

动物类
{
    ...
};


在您的版本中,Treatment.h和Animal.h相互包含。您需要使用前向声明解决此循环依赖关系。在.cpp文件中,包括所有必要的h文件。

09-08 09:35