c++ - 类型定义问题

标签 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.
}

最佳答案

此声明不正确:

void add(int,int,Array &);

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

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

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

关于c++ - 类型定义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10174258/

相关文章:

c++ - 使用 ncurses 绘制彩色框

c++ - 将自定义字符串放入文件中

c++指针从未初始化仍然输出为已初始化

Python:通过字符串列表递归 - 如何区分?

haskell - 在 Haskell 中将两个类合并/合并为一个类

c++ - 使用 Boost 查找大图的连接组件

mysql - 哈希密码字段使用什么数据类型以及长度?

c 使用结构指针数组写入和读取文件

arrays - Golang,将嵌入式结构转换为数组

c++ - 如何通过 C++ 中构造函数的初始化列表来初始化类的私有(private)结构成员?