VC++6 error C2059: syntax error : 'constant'

丶灬走出姿态 提交于 2019-12-01 08:10:47

Should be:

class ok
{
public:
    Strg v;
    ok() : v(45) {}
};

Non-static member variables that don't have default constructors (v in this case) should be initialized using initialization lists. In functions (like main) on the other hand you can use the regular constructor syntax.

What the compiler is complaining about is that you are trying to provide instruction on how ton instantiate the class member v inside your class definition, which is not allowed.

The place to instantiate v would be inside the contructor, or in the constructor's initializer list. For example:

Inside constructor:

class ok
{
public:
    Strg v;
    ok() {
        v = Strg(45);
    }
};

In initializer list:

class ok
{
public:
    Strg v;
    ok() : v(45) {}
};

The correct way to do it is the last one (otherwise, v would also require a default constructor and would be initialized twice).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!