3

There are two classes defined..

    class Dictionary
    {
    public:
        Dictionary();
        Dictionary(int i);
// ...
    };

and

    class Equation
    {
        static Dictionary operator_list(1);
// ...

    };

but the problem is, whenever I compile this, I get a weird error message

error C2059: syntax error : 'constant'

But it compiles well when I use the default constructor on operator_list.

0

3 Answers 3

3

In C++ you cannot combine declaration and initialization. When you do not specify constructor parameters of operator_list, you do not call its default constructor: you simply declare it. You need to also initialize it in the corresponding C++ file, like this:

Equation.h

class Equation {
    static Dictionary operator_list;
};

Equation.cpp:

Dictionary Equation::operator_list(1);

Note the absence of static in the CPP file: it is not there by design. The compiler already knows from the declaration that operator_list is static.

Edit: You have a choice with static constant members of integral and enumerated types: you can initialize them in the CPP file as in the example above, or you can give them a value in the header. You still need to define that member in your C++ file, but you must not give it a value at the definition time.

6
  • "In C++ you cannot combine declaration and initialization." What exactly do you mean by this statement ?
    – Mahesh
    Commented Dec 11, 2011 at 2:41
  • 1
    In fact, you can if you are doing it with an int : If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression
    – Drahakar
    Commented Dec 11, 2011 at 2:43
  • @Drahakar Here is my reference. Perhaps it's outdated, but that's exactly what I've learned some 15 years ago, and continue to use to this day. Commented Dec 11, 2011 at 2:51
  • 1
    @dasblinkenlight Drahakar refers to the C++ standard so, that's by definition more valid than your reference :). If equation was an int the OP's code would be valid.
    – FailedDev
    Commented Dec 11, 2011 at 3:02
  • 1
    The reference discusses static int. Static const int members can be initialized at declaration time, but a non-const static int member cannot. Commented Dec 11, 2011 at 3:26
2

static Dictionary operator_list(); is a function signature declaring a function returning a Dictionary and taking no arguments, that's why your compiler let you do it.

The reasons static Dictionary operator_list(1); fails is because you can't set a value of an complex type in the declaration of your classes. You need to do this elsewhere (e.g. in the .cpp )

For more information, see this post : https://stackoverflow.com/a/3792427/103916

2
#include <iostream>
using namespace std;

class Dictionary
{
public:
    Dictionary() {}
    Dictionary(int i):page(i) {}
    void display() { cout << "page is " << page << endl; }
private:
    int page;
};

class Equation
{
    public:
    static Dictionary operator_list;

};

Dictionary Equation::operator_list(1); // static members must be initialized this way...

int main()
{
Equation::operator_list.display();
}

Output is:

page is 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.