我有一个结构:
template <class T> struct Array{
int days;
T * M;
Array( int size ) : days(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
void currentDay();
void add(int,int,Array &);和一个类:
class Expe {
private:
int hk; //HouseKeeping
int fo; //Food
int tr; //Transport
int cl; //Clothing
int tn; //TelNet
int ot; //Others
}类构造器是:
Expe::Expe() {
this->hk = hk;
this->fo = fo;
this->tr = tr;
this->cl = cl;
this->tn = tn;
this->ot = ot;
}问题:在main函数中,我可以使用对象来操纵结构……例如,使用setObj()函数,但当我试图在控制器或Controller.h中定义我的函数时,我得到了禁止错误:
..\ListStruc.cpp:28:28: error: 'Array' is not a type
..\ListStruc.cpp: In function 'void add(int, int, int)':
..\ListStruc.cpp:31:4: error: request for member 'M' in 'A', which is of non-class type 'int'编辑:
void add(int cant, int tip,Array A){
//Adds to current day the amount to a specific type
A.M[currentDay]; // i try to use this object.
}发布于 2012-04-16 20:47:12
此声明不正确:
void add(int,int,Array &);因为Array是一个类模板,所以add函数也需要是一个模板:
template <class T>
void add(int,int,Array<T> &);此外,add函数的定义通过值接受参数,而声明通过引用接受参数。
https://stackoverflow.com/questions/10174258
复制相似问题