Click here to Skip to main content
15,844,602 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys, I have the following question:
I have a class with some CArrays in it. In another class, I need a function that returns instance of the first class. But I get a compile error C2248 (which I guess is because of the CArrays in it)

So he here is the example:
.h file
C++
class one
{
 public:
  one();
  virtual ~one();
  const one& operator=(const one& p);
  CArray <double,double&> array_one;
};

class two
{
 public:
 two();
 virtual ~two();
 one GetClass(CString C);
};


.cpp file

C++
...
 one two::GetClass(CString C){
  one class_one;
  if(C == "GO"){
     class_one.Add(5.4);
   }else{
    class_one.Add(3.5);
}
 return class_one;
}
...

Now is this correct, or I have to do the return like this
C++
void GetClass(CString C, one & class_one);


I know it may be a stupid question for most of you, but I want to make it clear to me :D
Posted
Updated 8-Apr-12 23:39pm
v2

If in doubt, search Google: Compiler Error C2248[^]

'member' : cannot access 'access' member declared in class 'class'
Members of a derived class cannot access private members of a base class. You cannot access private or protected members of class instances.
 
Share this answer
 
Comments
Lakamraju Raghuram 9-Apr-12 4:24am    
The OP made class one and class two members as public. And never mentioned about 'Add' method of class one.
OriginalGriff 9-Apr-12 4:52am    
Yes, I noticed.
I am assuming that the code fragment is a précis of the actual code, otherwise he would have referred to the line showing the compile error. Since the error number is the only solid fact we have...
Lakamraju Raghuram 9-Apr-12 5:20am    
And he has corrected it saying 'of course a typo' and it seems he knows already what c2248 means. A bit rude I felt.

Any way I think I got the problem. Please check the anwser.
"is of course a typo, it is "one" " - How do we know ?? We can't assume. Right ?

Now as you resolved it, here is my anwser :

CObject (from where CArray is derived from) declares a private copy constructor, so that constructor can't be used by default unless it's implemented in a derived class.

Now when you return CArray by value, then it's copy constructor and hence it's base CObject copy const is called (and this gives an error). Instead try to return as reference or pointer (which wont lead to calling of copy const)

[EDIT]

This is not a stupid question as mentioned by OP.
In fact an error made by many.
One cannot return any class object derived from CObject by value as it will lead to calling of copy constructor of CObject, which is not allowed
 
Share this answer
 
v2
Return by value in you function seems fine as the copy of the created object will be sent to the caller and nothing is problamatic. although if the size of the object is big then it might be a little performance overhead.

The second function declaration accepts a reference and you will assign that reference or change that reference values inside the function, which could work fine but this will make the caller resposible for creating the object and passing it by reference.

The convention is more like: pass all objects by const reference if you dont want them to get changed. pass them by pointer if you want them to get changed inside the function.

so I will say either you keep the original methid as is (little performance overhead on copy constructor call) or use pointer to do same thing.

function:
C++
void GetClass(CString C, one * class_one)
{
 class_one = new one();
  if(C == "GO")
   {
     class_one->Add(5.4);
   }
   else
   {
    class_one->Add(3.5);
   }
}


and call as:
C++
One *one;
GetClass(C, one);


I might be wrong, someone can perhaps correct me of ratify this.
 
Share this answer
 
Comments
Lakamraju Raghuram 9-Apr-12 5:08am    
With the performance in view you are 100% correct. But this doen't solve why the error is coming at the first instance. Check my anwser below for the reason of the error [ My second anwser, not the first anwser]. Thanks
C++
one two::GetClass(CString C)
{
   two class_one;
   .....
   return class_one;


When class two is not related to class one - how can you return object of two as one ?

And regarding that C2248 error look here : http://msdn.microsoft.com/en-us/library/tsbce2bh(v=vs.80).aspx

I suppose the error is nothing to do with CArrays
 
Share this answer
 
Guys I know what the C2245 stands for ;) But the funny thing is that if I make the function GetClass to return void (e.g. like I wrote the second version) the error doesn't appear.

The
C++
one two::GetClass(CString C)
{
  two class_one 
  ...
}

is of course a typo, it is "one".

Rahul Rajat Singh, thanks for the suggestion I will try using pointers, just not to forget to delete the *one after I finish with it :D
 
Share this answer
 
Well I thought it was obvious that is was a typo, but as you said one can't assume anything. Thanks for the answer, I will give it a try. Just to clear it, by saying to return a reference or pointer, you are speaking of something like this?

C++
one& GetClass(CString C)
and
C++
one* GetClass(CString C)
or as Rahul wrote?

P.S. What I wrote for the typo wasn't in a "attack" meaning, I just express my self like that :D
 
Share this answer
 
Comments
Lakamraju Raghuram 9-Apr-12 6:37am    
Never mind. Yes, the best way is to go for pointer or reference return. Good luck
xumepoc 9-Apr-12 6:46am    
Copied. Thanks again for the reply, it is appreciated :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900