why i get these errors? and how i avoid some warnings?

i have these code for i initializate the POINT structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Object3D: public SizePosition3D
{
public:
    POINT Vectors[6];
    Object3D(Point3D Position3D=0, Size3D Size3D=0)
    {
        Position=Position3D;
        Size=Size3D;
        Vectors=
        {
            { (Position.To2D().x * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) },
            { Size.To2D().x+Position.To2D().x, (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) },
            { Size.To2D().x+Position.To2D().x, Size.To2D().y+Position.To2D().y },
            { (Position.To2D().x * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), Size.To2D().y+Position.To2D().y },
            { (Position.To2D().x * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) }
        };
    }

};

error message: "assigning to an array from an initializer list"
why i can't assign on these way?
There is no copy assignment operator for plain arrays.
1
2
3
4
int foo[6];
int bar[6];
foo = bar; // error
for ( int i=0; i<6; ++i ) foo[i] = bar[i]; // ok "assignment of array" 


You can initialize an array.

Class Object3D does not have members "Position" or "Size".
Are they global variables or members of a base class?
If the latter, then why don't you initialize them in the constructor of the base class?
Last edited on
Because the language doesn't allow it. There isn't much more to say than that, really.

Since you're passing in the position and size as parameters, you can still initialize the "Vectors" array with the initializer list.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Foo {
  public:
    Foo(int i)
    : arr{i, i+1, i+2}
    { }
    
    int arr[3];
};

int main()
{
    Foo foo(42);
}
the best solution was using the array index:
1
2
3
4
5
Vectors[0] = { Position.To2D().x, Position.To2D().y};
        Vectors[1] = { Size.To2D().x + Position.To2D().x, Position.To2D().y};
        Vectors[2] = { Size.To2D().x + Position.To2D().x, Size.To2D().y+Position.To2D().y };
        Vectors[3] = { Position.To2D().x , Size.To2D().y+Position.To2D().y };
        Vectors[4] = { Position.To2D().x, Size.To2D().y};

now i must show you about the warning:
1
2
3
4
5
POINT to2D
        {
            (LONG) ( PositionX* Distance) / (Z + Distance),
            (LONG)(PositionY * Distance) / (Z + Distance)
        };

the PositionX and the others variables are float... but the POINT members are LONG...
warning: "warning: narrowing conversion of '((float)(LONG)(((Point3D*)this)->Point3D::PositionX * Distance) / (Z + Distance))' from 'float' to 'LONG' {aka 'long int'} inside { } [-Wnarrowing]|"
can i avoid these warning? i tried the casting conversion, but the warning remains :(
Casting has a higher precedence than division.
It's doing (LONG) ( PositionX* Distance), and then doing the division, and then setting the result.

You probably want something like:
(LONG)(PositionX * Distance / (Z + Distance))
Last edited on
thank you so much for correct me.
i never knew about the high precedence with casting.
thank you so much for all
Topic archived. No new replies allowed.