jave.web
jave.web

Reputation: 15052

C++ static struct init - item does not name a type

I am trying to put my solution in a static class, which is defined like:

class myDataParser{
    private:

    public:
    struct myData{
        struct index {
            static int item1,item2,item3,item4;
        };
        static char delimiter;
    };

};

But when I try to init the items in the main program like this:

myDataParser::myData::delimiter = ';';
myDataParser::myData::index::item1 = 0;
myDataParser::myData::index::item2 = 1;
myDataParser::myData::index::item3 = 2;
myDataParser::myData::index::item4 = 3;

I get error:

'item1' in 'struct myDataParser::myData::index' does not name a type

...same goes for delimiter and item2-4, what am I doing wrong? How would I set these members correctly?

Upvotes: 1

Views: 3053

Answers (2)

Laura Maftei
Laura Maftei

Reputation: 1863

From the standard (N3690): 9.4.2. §2

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class (3.3.7). [ Example:

class process {
    static process* run_chain;
    static process* running;
};
process* process::running = get_main();
process* process::run_chain = running;

The static data member run_chain of class process is defined in global scope; the notation process ::run_chain specifies that the member run_chain is a member of class process and in the scope of class process. In the static data member definition, the initializer expression refers to the static data member running of class process. — end example ] [ Note: Once the static data member has been defined, it exists even if no objects of its class have been created. [ Example: in the example above, run_chain and running exist even if no objects of class process are created by the program. — end example ] — end note ]

Upvotes: 2

P0W
P0W

Reputation: 47824

Prefix the data types char, int , etc for defining the static members :-

char myDataParser::myData::delimiter = ';';
~~~
int myDataParser::myData::index::item1 = 0;
~~~

//....

Upvotes: 6

Related Questions