gpt4 book ai didi

c++ - 这在 C++ 中可能吗? toString(类名*类)

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:24 24 4
gpt4 key购买 nike

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

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

治疗.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

治疗.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 类中。我不确定,但我认为这是因为 Animal 有 vector 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"   remove thisclass Treatmemt;    // forward declarationclass Animal{    ...};

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

关于c++ - 这在 C++ 中可能吗? toString(类名*类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7632437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com