Solved

Error C2248: cannot access private struct declared in class

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

19 Experts available now in Live!

Get 1:1 Help Now