简体   繁体   中英

Syntax error 'constant error

I wrote a class which has a constructor that takes 2 parameters, but when I try to use an object of the class with those parameters I get that syntax error, I have tried everything I know to solve this but I couldn't. the code:

class Vector2D{
public: 
Vector2D(int  xx, int yy) {}
Vector2D d(0, 0);
};

the error:

Error C2059 syntax error: 'constant' Project1

If I understand what you are shooting for, the way you'd write the class is as follows

class Vector2D
{
public: 
    Vector2D() = default;
    Vector2D(int xx, int yy) : m_xx(xx), m_yy(yy) {}
private:
    int m_xx = 0;
    int m_yy = 0;
};

Your current issue is this line

Vector2D d(0, 0);

It looks like you are trying to declare a member variable d which is an instance of the class you are trying to define.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM