Solved

Error C2248: cannot access private struct declared in class

Posted on 2006-10-27
4
415 Views
Last Modified: 2008-01-16
Hi expert,

I don't understand why the following code is not able to compile on Windows, I was able to compile it on redhat 3.0.

ClassFreeMem<ClassOne::ClassTwo>*
      ClassOne::ClassTwo::freememory= 0;
ClassFreeMem<ClassOne>* coneClass::freememory= 0;
struct InitFreeMemPtrs
{
   InitFreeMemPtrs(void)       // Default constructor.
   {
      // Allocate the freestore in the correct order.
      ClassOne::ClassTwo::freememory= new ClassFreeMem<ClassOne::ClassTwo>;  -> compile error here C2248
      ClassOne::freememory= new ClassFreeMem<ClassOne>;
   }

   ~InitFreeMemPtrs(void)      // Default destructor.
   {
      // Delete the freestore in the correct order.
      delete ClassOne::freememory;
      delete ClassOne::ClassTwo::freememory;  -> compile error here C2248
   }
};

InitFreeMemPtrs InitFreeMem;


ClassOne
{
public:
 static FlFreeStoreClass<ClassOne>* freememory;
private:
struct ClassTwo
{
static ClassFreeMem<ClassTwo>* freememory;
}
}


If I move the struct ClassTwo in the public area, then I am able to compile.  At this point, I would like to find a better solution than moving the struct ClassTwo to public.  Why this is able to compile on Linux, but not windows?

Looking foward to hear from you.
0
Comment
Question by:4eyesgirl
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17820359
I am not sure why that compiles on Linux (it shouldn't), but making 'InitFreeMemPtrs' a 'friend' of 'ClassOne' should work, i.e.

ClassOne
{
friend struct InitFreeMemPtrs;
public:
 static FlFreeStoreClass<ClassOne>* freememory;
private:
struct ClassTwo
{
static ClassFreeMem<ClassTwo>* freememory;
}
}


0
 

Author Comment

by:4eyesgirl
ID: 17820520
Jkr -

Do you know the compile did let this line of code compile?  
ClassFreeMem<ClassOne::ClassTwo>*
      ClassOne::ClassTwo::freememory= 0;
What is the difference between this one than the one in the constructor?


0
 
LVL 86

Expert Comment

by:jkr
ID: 17820641
Shouldn't compile either, but may (erroneously) look like a static member initializer to the compiler. Weird.
0
 

Author Comment

by:4eyesgirl
ID: 17820648
Thanks jkr.

As alwasy - you are a great help!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 158
What is the real difference between C++ and Visual C++.NET? 7 120
How to gracefully close the c++ 11 thread? 3 107
What is sub-make ? 2 71
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question