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.

Null
  • 1,950
  • 9
  • 30
  • 33
Jaebum
  • 1,397
  • 1
  • 13
  • 33

3 Answers3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • **"In C++ you cannot combine declaration and initialization."** What exactly do you mean by this statement ? – Mahesh Dec 11 '11 at 02: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 Dec 11 '11 at 02:43
  • @Drahakar Here is [my reference](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11). Perhaps it's outdated, but that's exactly what I've learned some 15 years ago, and continue to use to this day. – Sergey Kalinichenko Dec 11 '11 at 02: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 Dec 11 '11 at 03: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*. – StilesCrisis Dec 11 '11 at 03:26
  • @FailedDev I guess I'm dating myself here, but I learned that trick before the first C++ ISO standard has been approved. I updated the answer to mention the alternative available for constant integral/enum types. Thanks! – Sergey Kalinichenko Dec 11 '11 at 03:35
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

Community
  • 1
  • 1
Drahakar
  • 5,986
  • 6
  • 43
  • 58
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
Sanish
  • 1,699
  • 1
  • 12
  • 21