开发者

Visual C++ weird errors

I copied a开发者_StackOverflow project I did in g++ to visual studio and it threw up a lot of errors, most of them concentrate on using the { a, b, c } syntax to initialize structs.

The problem its having right now is it can't understand this:

struct WallPoint
{
    int x, y;
};

WallPoint p = { 0, 1 }; // error C2059: syntax error : '{'

or

WallPoint p2[] = { { 0, 1 }, { 1, 2 } }; // error C2334: unexpected token(s) preceding '{'; skipping apparent function body

Is there any compiler switch I can use to make it understand this?

Edit: The whole file that doesn't work:

struct WallPoint
{
    int x, y;
};

void x()
{
    WallPoint p;
    p = (WallPoint) { 0, 1 };
}

class
{
    WallPoint p[] = 
    {
        { 0, 1 },
        { 1, 2 }
    };
};

int main() { return 0; }


You need the activate the mental switch "semicolon after class definition":

struct Wallpoint { int x, y; } ;
                              ^^^

Update, to answer your new, entirely different question:

Aggregate initialization can only be used to initialise named variables:

WallPoint p = { 0, 1 }; // OK

It can not be used for assignments, or for temporaries:

p = { 0, 1 };            // No!
p = (WallPoint) { 0, 1 } // Nooo!

p = WallPoint{0, 1}; // New in C++11


You're missing the ; at the end of the structure declaration.


You are using an assignment rather than an initializer. It should be:

WallPoint p = { 0, 1 };

I presume that in the second example the class is meant to be a function. If so then the initializer there is fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜