gpt4 book ai didi

c++ - 如何将指向结构的指针传递给构造函数?

转载 作者:行者123 更新时间:2023-11-28 01:20:27 26 4
gpt4 key购买 nike

我想将指向结构的指针(此结构的变量是一个数组,其中的元素具有值 x 和 y)传递给构造函数。接下来我想将这个结构的每个变量的值 x 和 y 分配给类中结构的类似变量值。

class Convex_quadrliteral
{
protected:
struct VC {
float x, y;
} vertice_coordinate[4];

public:
Convex_quadrliteral (VC *pointerVC);
};

Convex_quadrliteral::Convex_quadrliteral (VC *pointerVC) {
cout << "\nObject is being created" << endl;

for (int i = 0; i < 4; i++) //variable initialisation
{
vertice_coordinate[i].x = pointerVC[i].x;
vertice_coordinate[i].y = pointerVC[i].y;
}

//object's properties output
cout << "Properties: " << endl
<< "A (" << vertice_coordinate[0].x << ", " << vertice_coordinate[0].y << ")" << endl
<< "B (" << vertice_coordinate[1].x << ", " << vertice_coordinate[1].y << ")" << endl
<< "C (" << vertice_coordinate[2].x << ", " << vertice_coordinate[2].y << ")" << endl
<< "D (" << vertice_coordinate[3].x << ", " << vertice_coordinate[3].y << ")" << endl;
}

int main()
{
struct vertice_coordinate
{
float x, y;
};

vertice_coordinate *pointerVC = new vertice_coordinate[4];

for (int i = 0; i < 4; i++) {
pointerVC[i].x = 2;
pointerVC[i].y = 2;
}

Convex_quadrliteral figure_1(pointerVC);

我期望输出:甲(2, 2)乙(2, 2)C(2, 2)D(2, 2)

输出错误:没有声明匹配 'Convex_quadrliteral::Convex_quadrliteral(Convex_quadrliteral::VC*)'C onvex_quadrliteral::Convex_quadrliteral (VC *pointerVC)

最佳答案

您重新定义了 VC 结构,即使结构看起来相同,编译器也会将它们视为两种不同的类型。定义一个结构并在您的类和 main 中使用它。

关于c++ - 如何将指向结构的指针传递给构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com