gpt4 book ai didi

c++ - 如何在 C++ 中的两个类之间使用 toString 和 vector?

转载 作者:行者123 更新时间:2023-11-30 02:07:36 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Is this possible in C++? toString(ClassName* class)

我正在尝试使用 toString但我在那里有问题。我第二次问这个问题,因为我之前第一次问的信息不够。我只是担心问题的长度。对于那个很抱歉。

toString 在 Animal 中类,和 Animal类有vector<Treatment> treatArray;作为数据成员,但问题是我可以使用数据成员本身,但我无法获取 treatArray 的数据成员.这些是我的代码:

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

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

每个类都有setter和getter,我删减了。

此外,我认为这是因为 vector 的初始化,但我在谷歌上搜索了初始化 vector ,它说 vector 是自动初始化的,所以我不必手动初始化。现在我不知道问题是什么:(错误信息是:

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

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