C++ 将数组传递到对象方法
我遇到了一个基本的数组问题,我似乎无法理解 我有一个类“StaticDisplayLayer”,构造函数接受2个参数——一个int和一个指向3个无符号短int数组的指针:C++ 将数组传递到对象方法,c++,arrays,parameter-passing,C++,Arrays,Parameter Passing,我遇到了一个基本的数组问题,我似乎无法理解 我有一个类“StaticDisplayLayer”,构造函数接受2个参数——一个int和一个指向3个无符号短int数组的指针: //constructor definition: StaticDisplayLayer(int type, unsigned short *displayColor[3]); //constructor code: StaticDisplayLayer::StaticDisplayLayer(int type, unsig
//constructor definition:
StaticDisplayLayer(int type, unsigned short *displayColor[3]);
//constructor code:
StaticDisplayLayer::StaticDisplayLayer(int type, unsigned short *dColor[3]) : DisplayLayer(type)
{
displayColor = dColor;
}
我正在尝试使用以下方法创建该类的实例:
unsigned short layerColor[3] = {(unsigned short)255,(unsigned short)255,(unsigned short)255};
StaticDisplayLayer myLayer(1, &layerColor);
我的理解是&layerColor是指向layerColor数组的指针,但编译器给了我以下错误:
no matching function for call to `StaticDisplayLayer::StaticDisplayLayer(int, short unsigned int (*)[3])'
Candidates are:
StaticDisplayLayer::StaticDisplayLayer(const StaticDisplayLayer&)
StaticDisplayLayer::StaticDisplayLayer(GLenum, short unsigned int**)
我知道第二个候选者是我正在尝试使用的,但显然我不理解数组指针的概念。如果有人能解释如何调用该构造函数和/或解释这一点的任何资源,我将不胜感激——到目前为止,我的在线搜索并没有得到太多的结果。
unsigned short*dColor[3]
不是指向数组的指针,而是指向指针数组的指针。[3]
将被忽略并替换为另一个指针,因为它是一个函数参数。换句话说,它会腐烂。要创建指向数组的指针,请使用无符号短(*dColor)[3]
,这是指向大小为3的数组的指针,指向无符号短(
)
C++中更好的方法是使用数组的引用:
unsigned short (&dColor)[3]
只需传递
layerColor
unsigned short*dColor[3]
不是指向数组的指针,而是指向指针数组的指针。[3]
将被忽略并替换为另一个指针,因为它是一个函数参数。换句话说,它会腐烂。要创建指向数组的指针,请使用无符号短(*dColor)[3]
,这是指向大小为3的数组的指针,指向无符号短(
)
C++中更好的方法是使用数组的引用:
unsigned short (&dColor)[3]
只需传递
layerColor
&layerColor
的类型指针指向3个无符号的短字符数组
。另一方面,构造函数需要unsigned short*[3]
,它是指向unsigned short的三个指针的数组。事实上,作为函数参数类型,它是指向无符号short的指针-维度被完全忽略。我想你的意思是,你的构造函数只需要一个指向无符号short的指针,然后传递layerColor
StaticDisplayLayer(int type, unsigned short *displayColor);
unsigned short layerColor[3] = {255,255,255};
StaticDisplayLayer myLayer(1, layerColor);
请注意,在这种情况下,调用方负责传递至少包含3个元素的数组。或者,您可以通过
StaticDisplayLayer(int type, unsigned short (&displayColor)[3]);
但在这种情况下,您将无法传递动态分配的数组
<>我注意到你在C++中缺少一些基本知识,所以我建议你读一个 <代码>和LayelSux<代码>有3个无符号短裤的数组<代码>指针。另一方面,构造函数需要unsigned short*[3]
,它是指向unsigned short的三个指针的数组。事实上,作为函数参数类型,它是指向无符号short的指针-维度被完全忽略。我想你的意思是,你的构造函数只需要一个指向无符号short的指针,然后传递layerColor
StaticDisplayLayer(int type, unsigned short *displayColor);
unsigned short layerColor[3] = {255,255,255};
StaticDisplayLayer myLayer(1, layerColor);
请注意,在这种情况下,调用方负责传递至少包含3个元素的数组。或者,您可以通过
StaticDisplayLayer(int type, unsigned short (&displayColor)[3]);
但在这种情况下,您将无法传递动态分配的数组
<>我注意到你在C++中缺少一些基本知识,所以我建议你读一个无符号短DeSpCooSoice [3 ],它是一个无符号短指针或无符号短*的数组。
无符号short layerColor[3]是一个无符号短指针数组,而不是一个无符号短指针数组
相反,我会为color创建一个结构或类,然后传递一个指针或对它的引用。unsigned short displayColor[3]实际上是指一个无符号短指针数组或无符号短*数组
无符号short layerColor[3]是一个无符号短指针数组,而不是一个无符号短指针数组
相反,我会为color创建一个结构或类,然后传递一个指针或一个引用。您是否考虑过使用std::vector
?你可以用它绕过所有那些复杂的语法。是的,我正要用vector重写它,但不想在不理解我做错了什么的情况下就这样放弃。你有没有考虑过用std::vector
?你可以用它来绕过所有复杂的语法。是的,我打算用矢量重写它,但不想让它消失,而不理解我做错了什么。我有Stroustrup的书,按顺序:)西奥:Lippman的C++底漆更好。“Stroustup的书不是教科书,而是Stroustrup的书的一个参考文献:”西奥克斯:Lippman的C++底漆更好。Stroustup不是一本教科书,而是一本参考书