fotinsky
fotinsky

Reputation: 992

Defining static two-dimensional array as private

I had a two-dimenensional vector as member variable, and initialized it by the constructor. Now that I have to declare it as static, I get compiler errors for wrong syntax.

It's declared and defined as that:

 std::vector< std::vector<int> > knowledge( 1, std::vector<int>(1, 0) );

in the private part of the class.

I get the compiler errors on that line:

expected identifier before numeric constant
expected »,« or »...« before numeric constant

Where is the mistake?

Upvotes: 0

Views: 156

Answers (3)

crayzeewulf
crayzeewulf

Reputation: 6010

Read this then do this:

// 
// In Foo.h...
// 
#include <vector>

class Foo {
    // ...
private: 
    static std::vector< std::vector<int> > knowledge ;
} ;

// 
// In Foo.cpp...
//
std::vector< std::vector<int> > Foo::knowledge(1, std::vector<int>(1, 0));

Upvotes: 1

A.B.
A.B.

Reputation: 16640

Static class members need to be declared inside the class, but defined outside. Example

class C {
    static std::vector<std::vector<int>> knowledge;
};

std::vector<std::vector<int>> C::knowledge( 1, std::vector<int>(1, 0) );

Upvotes: 1

k.v.
k.v.

Reputation: 1223

For using static class member you have to define this member outside a class, so compiler will allocate it in memory.

After you define a corresponding variable outside a class, you can initialize and use it.

Upvotes: 1

Related Questions