OOP, assignment operator does not work
This 开发者_高级运维is from my code:
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;
}
My problem is, that for Place = XYZ;, it's showing the error "invalid use of member (did you forget the '&'?)" and for Speed.x = V.x; "insufficient contextual information to determine type".
Your Place, Speed and Acc members are declared as functions. Use:
struct Bodies
{
int Mass;
float Dist[100];
R3 Place;
R3 Speed;
R3 Acc;
instead.
And use initialization in the constructor:
Bodies::Bodies(int M, R3 XYZ, R3 V, R3 A): Mass(M), Place(XYZ), Speed( V ), Acc( A ){}
instead of assignment.
Place
is a member function taking three floats
, and you are trying to assign to it in the constructor. You can't do that. Perhaps you meant for Place
to be a data member of type R3
:
struct Bodies
{
...
R3 Place;
R3 Speed;
...
};
These are functions declarations :
R3 Place(float, float, float);
R3 Speed(float, float, float);
R3 Acc(float, float, float);
not object creations.
精彩评论