tags:

views:

57

answers:

4

I'm writing an app in Visual Studio C++ and I have problem with assigning values to the elements of the array, which is array of elements of structure type. Compiler is reporting syntax error for the assigning part of the code. Is it possible in anyway to assign elements of array which are of structure type?

typedef struct {
    CString x;
    double y;
} Point;


Point p[3];
p[0] = {"first", 10.0};
p[1] = {"second", 20.0};
p[2] = {"third", 30.0};
+5  A: 

Give your struct a constructor:

struct Point {
    CString x;
    double y;
   Point( const CString & s = "" , double ay = 0.0 ) : x(s), y(ay) {}
};

You can then say:

Point p[3];
p[0] = Point( "first", 10.0 );
anon
+1  A: 

You can not set your data in this way. Instead write:

p[0].x = "first": p[0].y = 10.0;
...
Danvil
+4  A: 

You can use an initializer when the array is being declared:

struct Point{
    CString x;
    double y;
};

Point p[3] = {
  {CString("first"), 10.0},
  {CString("second"), 20.0},
  {CString("third"), 30.0}
};

But not on assignment.

Brian R. Bondy
@Brian My bad. I made a mistake when testing it.
Yacoby
A: 

What Neil says is right indeed!!

Some more info here...

CVS-2600Hertz