类型定义问题

时间:2012-04-16 12:40:13

标签: c++ types struct

我有一个结构:

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或Controller.hi中定义我的函数时,得到以下错误:

..\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.
}

1 个答案:

答案 0 :(得分:2)

此声明不正确:

void add(int,int,Array &);

由于Array是一个类模板,add函数也需要是一个模板:

template <class T>
void add(int,int,Array<T> &);

此外,您对add函数的定义按值获取参数,而声明则通过引用获取参数。