Solved

Error C2248: cannot access private struct declared in class

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now