我有一个项目要做。它有一个Class Temp,它有几个函数。还有另一个Class Weather,它将数据存储在一个名为Observation的结构中。这个结构有一个,它的字段类型为class Temp。
Class Temp {
Temp();
Temp(string t);
string getTemp();
void setTemp(string temp);
};
Class Weather
{ @beta
在我的Weather类中,我有一些调用Temp类的函数
// Weather.h
public:
void record(Temp temp, float d); // error 'Temp' has not been declared
Temp getTemp() const; // 'Temp' does not name a type
protected:
struct Observation
{
Temp t;
int deg;
};
Observation obs[20];
};
// Weather.cpp
void Temp::record(Temp temp, float d){
obs[i].temp = temp;
obs[i].deg = d;
i++;
}我已经尝试创建了Temp和Weather的实例。但是我无法到达结构中的Temp t。
int main(){
Temp t;
Weather w;
w.record(tt,dd);
}请指引我。
发布于 2014-03-26 11:12:06
这一行:
obs[i].tp;没有任何意义,因为Observation没有名为tp的字段。试试这个:
int record(Temp tp, float d){
obs[i].t = tp;
obs[i].deg = d;
i++;
}
...
int main(){
Temp t;
Weather w;
int dd=7;
w.record(t,dd);
}发布于 2014-03-26 11:48:17
为了让你的struct能够访问Temp,你需要将Temp设为公共的。试试这个:
class Temp {
public:
Temp();
Temp(string t);
string getTemp();
void setTemp(string temp);
};现在,要在main中访问Weather,您需要做同样的事情。
class Weather{
int i;
public:
// method to used to record all values
int record(Temp tp, float d){
obs[i].t = tp;
obs[i].deg = d;
i++;
}此外,在main中,使用以下代码行将未定义的变量传递给Weather:
w.record(tt,dd);https://stackoverflow.com/questions/22650701
复制相似问题