这是从我的代码:

struct R3
{
       float x;
       float y;
       float z;

       R3(float, float, float);
       R3();
};

R3::R3(float a, float b, float c)
{
        x = a;
        y = b;
        z = c;
}

R3::R3()
{
        x = 0;
        y = 0;
        z = 0;
}

struct Bodies
{
       int Mass;
       float Dist[100];
       R3 Place(float, float, float);
       R3 Speed(float, float, float);
       R3 Acc(float, float, float);

       Bodies(int, R3, R3, R3);
};

Bodies::Bodies(int M, R3 XYZ, R3 V, R3 A)
{
       Mass = M;
       Place = XYZ;
       Speed.x = V.x;
       Speed.y = V.y;
       Speed.z = V.z;
       Acc.x = A.x;
       Acc.y = A.y;
       Acc.z = A.z;
}


我的问题是,对于Place = XYZ ;,它显示错误“成员的无效使用(您是否忘记了'&'?)”
并且对于Speed.x = V.x; “上下文信息不足,无法确定类型”。

最佳答案

您的Place,Speed和Acc成员被声明为函数。
采用:

struct Bodies
{
       int Mass;
       float Dist[100];
       R3 Place;
       R3 Speed;
       R3 Acc;


代替。

并在构造函数中使用初始化:

Bodies::Bodies(int M, R3 XYZ, R3 V, R3 A): Mass(M), Place(XYZ), Speed( V ), Acc( A ){}


而不是分配。

关于c++ - OOP,赋值运算符不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4004217/

10-11 18:54