我想创建一个C++类,它接受一个“向量”整数t9作为参数。我不明白为什么我的代码不能工作:
header的内容如下:
class Matrice3x3{
public:
int n;
int t[9];
Matrice3x3 inverse();
float det();
void print();
Matrice3x3(int u = 3);
Matrice3x3(int p[9], int m = 9);
private:
};这里是类描述的内容:
Matrice3x3::Matrice3x3(int u) {
n = u*u;
}
Matrice3x3::Matrice3x3(int p[9] , int m){
n = m;
t = p;
}我得到了这个错误:
In constructor 'Matrice3x3::Matrice3x3(int*, int)':
incompatible types in assignment of 'int*' to 'int [9]'
t = p;
^我只是不明白我说的其中一个[]是一个指针...
感谢您的回复!
发布于 2014-01-05 23:18:59
您不能像这样复制数组。您应该逐个元素地复制它们,或者使用memcpy。在一般情况下,最好对容器使用标准库(在本例中为std::vector)。您应该有充分的理由选择C风格的数组,而不是标准容器。
发布于 2014-01-05 23:40:19
好吧,如果你真的想要固定大小的数组,可以使用C++11的std::array<> (Boost也有一个针对pre C++11的定义)。您可以将其分配为std::array < int,9>,然后将其作为对象传递。您还可以使用size()成员函数来获取元素的数量(尽管它是硬编码在类型中的),并且它还有其他成员函数(例如begin()和end()),这使得它看起来像一个std容器,所以您也可以在它上使用std的算法。基本上,是一个固定大小的数组的包装器。当然,你可以通过引用的值来传递。
https://stackoverflow.com/questions/20935167
复制相似问题