【发布时间】:2013-02-18 00:50:23
【问题描述】:
我正在尝试为类调用创建一个构造函数,其中 4 个数组作为参数传递。我试过使用*,& 和数组本身;但是,当我将参数中的值分配给类中的变量时,出现此错误:
call.cpp: In constructor ‘call::call(int*, int*, char*, char*)’:
call.cpp:4:15: error: incompatible types in assignment of ‘int*’ to ‘int [8]’
call.cpp:5:16: error: incompatible types in assignment of ‘int*’ to ‘int [8]’
call.cpp:6:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’
call.cpp:7:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’
感谢您帮助我找出我的错误并帮助我纠正它。 这是我的代码:
.h 文件
#ifndef call_h
#define call_h
class call{
private:
int FROMNU[8];
int DESTNUM[8];
char INITIME[14];
char ENDTIME[14];
public:
call(int *,int *,char *,char *);
};
#endif
.cpp 文件
call:: call(int FROMNU[8],int DESTNUM[8],char INITIME[14],char ENDTIME[14]){
this->FROMNU=FROMNU;
this->DESTNUM=DESTNUM;
this->INITIME=INITIME;
this->ENDTIME=ENDTIME;
}
【问题讨论】:
-
如果您不支持 C++11,请将数组替换为
std::array或std::tr1::array(或者,boost::array) -
数组与指针不同,尽管在许多情况下它们被降级为指针。对于您的用例,请考虑使用
std::array而不是[]数组。 -
你知道变量可以是小写的,对吧?
标签: c++ arrays pointers parameters constructor