Error VC ++ 6 C2059: syntax error: "constant"

Made this simple class with MSVC ++ 6.0

class Strg
{
public:
    Strg(int max);
private:
    int _max;
};


Strg::Strg(int max)
{
  _max=max;
}

Sounds good if I use it in:

main()
{
  Strg mvar(10);
}

But now, if I use it in another class:

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

I get the error message: error C2059: syntax error: constant

Could you tell me more please?

+3
source share
2 answers

Must be:

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

Non-static member variables that do not have default constructors (v in this case) must be initialized using initialization lists . On the other hand, in functions (like main) you can use regular constructor syntax.

+7
source

, , , ton v , .

v . :

:

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

:

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

- ( v ).

+4

Source: https://habr.com/ru/post/1781833/


All Articles