Solved

Error C2248: cannot access private struct declared in class

Posted on 2006-10-27
4
414 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

810 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