将数组传递给对象方法

发布于 2024-12-21 08:55:00 字数 1035 浏览 1 评论 0原文

我遇到了一个基本的数组问题,我似乎无法解决这个问题。

我有一个类“StaticDisplayLayer”,构造函数需要 2 个参数 - 一个 int 和一个指向 3 个无符号短整型数组的指针:

//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**)

我知道第二个候选者是我正在尝试使用的,但显然我不理解指向数组的指针的概念。如果有人可以阐明如何调用该构造函数和/或解释这一点的任何资源,我将不胜感激 - 到目前为止,我的在线搜索还没有真正出现太多。

I'm running into a basic array issue that I can't seem to get my head around.

I have a class "StaticDisplayLayer" and the constructor takes 2 parameters - an int and a pointer to an array of 3 unsigned short ints:

//constructor definition:
StaticDisplayLayer(int type, unsigned short *displayColor[3]);

//constructor code:
StaticDisplayLayer::StaticDisplayLayer(int type, unsigned short *dColor[3]) : DisplayLayer(type)
{
    displayColor = dColor;
}

I'm trying to create an instance of that class using the following:

unsigned short layerColor[3] = {(unsigned short)255,(unsigned short)255,(unsigned short)255};
StaticDisplayLayer myLayer(1, &layerColor);

My understanding is that &layerColor is a pointer to the layerColor array but the compiler is giving me the following error:

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**)

I know the second candidate is the one I'm trying to use but obviously I'm not understanding the concept of pointer to an array. If someone could shed some light on how to call that constructor and/or any resources that explain this I'd appreciate it - so far my searches online haven't really turned up much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇柔作态 2024-12-28 08:55:00

unsigned Short *dColor[3] 不是指向数组的指针,而是指向指针数组的指针。 [3] 被忽略并替换为另一个指针,因为它是函数参数。换句话说,它腐烂。要创建指向数组的指针,请使用 unsigned Short (*dColor)[3],它是一个指向大小为 3 到 unsigned Short 的数组的指针。

C++ 中更好的想法是使用对数组的引用:

unsigned short (&dColor)[3]

并且只需传递layerColor

unsigned short *dColor[3] is not a pointer to an array, it's a pointer to an array of pointers. The [3] is ignored and replaced with another pointer, as it is a function parameter. In other words, it decays. To make a pointer to an array, use unsigned short (*dColor)[3], which is a pointer to array of size 3 to unsigned short.

An even better idea in C++ is to use a reference to an array:

unsigned short (&dColor)[3]

And just pass layerColor.

朦胧时间 2024-12-28 08:55:00

&layerColor 具有类型指向 3 个无符号 Shorts 数组的指针。另一方面,您的构造函数需要 unsignedshort *[3],它是指向 unsignedshort 的三个指针的数组。事实上,作为函数参数类型,它是一个指向无符号短整型的指针 - 维度被完全忽略。我认为你的意思是让你的构造函数只接受一个指向 unsigned Short 的指针并传递 layerColor

StaticDisplayLayer(int type, unsigned short *displayColor);

unsigned short layerColor[3] = {255,255,255};
StaticDisplayLayer myLayer(1, layerColor);

请注意,在这种情况下,调用者有责任传递一个至少包含 3 个元素的数组。或者,您可以通过以下方式使函数接受恰好 3 个 ushort 的数组,

 StaticDisplayLayer(int type, unsigned short (&displayColor)[3]);

但在这种情况下,您将无法传递动态分配的数组。

我不能不注意到你缺乏一定的C++基础知识,所以我建议你阅读一本不错的 C++ 书

&layerColor has type pointer to array of 3 unsigned shorts. Your constructor, on the other hand, expects unsigned short *[3] which is array of three pointers to unsigned short. As a matter of fact, as a function parameter type, it's a pointer to pointer to unsigned short - the dimension is ignored completely. I think what you meant was for your constructor to take just a pointer to unsigned short and pass layerColor

StaticDisplayLayer(int type, unsigned short *displayColor);

unsigned short layerColor[3] = {255,255,255};
StaticDisplayLayer myLayer(1, layerColor);

Note that in this case it is the caller's responsibility to pass an array that has at least 3 elements in it. Alternatively you could make the function take an array of exactly 3 ushorts by means of

 StaticDisplayLayer(int type, unsigned short (&displayColor)[3]);

But in this case you won't be able to pass dynamically allocated arrays.

I cannot but notice that you lack certain basic knowledge in C++, so I suggest you to read a good C++ book

感情洁癖 2024-12-28 08:55:00

unsigned Short displayColor[3] 本质上是指一个无符号短指针数组或无符号短*

unsigned Short layerColor[3] 是一个无符号短整型数组,而不是一个无符号短指针数组。

相反,我会为颜色创建一个结构或类,然后传递一个指针或引用。

unsigned short displayColor[3] is saying an array of unsigned short pointers or unsigned short * essentially.

unsigned short layerColor[3] is an array of unsigned shorts, not an array of unsigned short pointers.

I would instead make a struct or class for color and then pass a pointer or reference to one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文