views:

97

answers:

3

Hi,

I have the following situation:

class Test
{
private:
    class SubType
    {
    //...
    };
    static std::vector<SubType> v;
};

Because v is static, I initilize it in the cpp file with

std::vector<Test::SubType> Test::v;

But this does not work, the compiler tells me that "Test::SubType" is private. What can I do about this?

Thanks!

+1  A: 

I guess you forgot to #include <vector>. Because the following compiles on comeau

#include <vector>
class Test { 
    class SubType 
    { 
    //... 
    }; 
    static std::vector<SubType> v; 
};
std::vector<Test::SubType> Test::v; 
Abhay
+2  A: 

This works for me:

#include <vector>
using namespace std;

class A {
    class B {
    };
    static  B b;
    static vector <B> vb;
};

A::B A::b;
vector <A::B> A::vb;
anon
A: 

Others reported the code compiles fine. I want to supply the Standard wording for backing it up. At 11/5

All access controls in clause 11 affect the ability to access a class member name from a particular scope. The access control for names used in the definition of a class member that appears outside of the member’s class definition is done as if the entire member definition appeared in the scope of the member’s class. [...]

[Example:

class A {
  typedef int I; // private member
  I f();
  friend I g(I);
  static I x;
};

A::I A::f() { return 0; }
A::I g(A::I p = A::x);
A::I g(A::I p) { return 0; }
A::I A::x = 0;

Here, all the uses of A::I are well-formed because A::f and A::x are members of class A and g is a friend of class A. This implies, for example, that access checking on the first use of A::I must be deferred until it is determined that this use of A::I is as the return type of a member of class A. ]

Johannes Schaub - litb