首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型定义问题

类型定义问题
EN

Stack Overflow用户
提问于 2012-04-16 20:40:13
回答 1查看 73关注 0票数 0

我有一个结构:

代码语言:javascript
复制
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 &);

和一个类:

代码语言:javascript
复制
class Expe {
private:
    int hk;     //HouseKeeping
    int fo;     //Food
    int tr;     //Transport
    int cl;     //Clothing
    int tn;     //TelNet
    int ot;     //Others

 }

类构造器是:

代码语言:javascript
复制
Expe::Expe() {
this->hk = hk;
this->fo = fo;
this->tr = tr;
this->cl = cl;
this->tn = tn;
this->ot = ot;
}

问题:在main函数中,我可以使用对象来操纵结构……例如,使用setObj()函数,但当我试图在控制器或Controller.h中定义我的函数时,我得到了禁止错误:

代码语言:javascript
复制
..\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'

编辑:

代码语言:javascript
复制
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.
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-16 20:47:12

此声明不正确:

代码语言:javascript
复制
void add(int,int,Array &);

因为Array是一个类模板,所以add函数也需要是一个模板:

代码语言:javascript
复制
template <class T>
void add(int,int,Array<T> &);

此外,add函数的定义通过值接受参数,而声明通过引用接受参数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10174258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档