Problem with Classes and Accessing Private Members
I am getting error C2248 - cannot access private member declared in class 'Complex'. I understand what the error means, but I don't understand why it is occurring, and thus how to fix it. This code snippet is part of a homework assignment, whose idea is overloading operators and constructors. I am a C++ newbie, so please bare that in mind in your explanations. I am NOT looking to have anyone do my homework for me, but I've hit a brick wall and need to understand why these errors are popping up.
Re: Problem with Classes and Accessing Private Members
Constructors of your class are private. Members of class are private by default, and you have no access specifier before your constructors.
Besides, there are other errors:
- cmath is correct C++ header, not math.h
- your logic of multiplying imaginyary part by sqrt(-1) is somewhat faulty. You just need to store a real value there, a coefficient before i.
- You might want to read some rules and practices about arothmetic (and not only) operator overloading: http://www.codeguru.com/forum/showthread.php?t=401691
Re: Problem with Classes and Accessing Private Members
Thanks for your reply. So Constructors are private, meaning any code inside of them is localized, right? If so, how does returning an anonymous class object violate accessing a Constructor?
I still don't understand how
Code:
return Complex(totalReal, totalImaginary);
has to do with accessing a constructor. How else can I return a class object in a function?
As for multiplying imaginary by sqrt(-1), are you suggesting that instead of the code:
Code:
pow(-1, .5)
I instead store that in a (global) variable "i" and use that instead?
Sorry if I sound like a retard, and thanks for the help.
Re: Problem with Classes and Accessing Private Members
Originally Posted by jason18241
So Constructors are private, meaning any code inside of them is localized, right?
No, it means that they are inaccessible to anyone except this class itself and its friends. Noone else can create objects of your class. I think that you want to be possible to create your objects anywhere, so you need to make your constructors public.
Originally Posted by jason18241
If so, how does returning an anonymous class object violate accessing a Constructor?
Object has to be constructed to be created. Your operators are global functions, and your class constructors cannot be accessed by them, because they are private.
Originally Posted by jason18241
I still don't understand how
Code:
return Complex(totalReal, totalImaginary);
has to do with accessing a constructor. How else can I return a class object in a function?[/code]
This code creates new object, so it needs to access a constructor. This is the correct way of returning object, but you need to make constructor accessible (which in this case means public).
Originally Posted by jason18241
As for multiplying imaginary by sqrt(-1), are you suggesting that instead of the code:
Code:
pow(-1, .5)
I instead store that in a (global) variable "i" and use that instead?
No. You just need to understand, that your class is just a model of the complex number. Complex number consists of real and imaginary part. In C++ there are no means to express these. However you might use numbers as coefficients for real and imaginary part. It is similar to vector notation for complex numbers, instead of algebraic notation: instead of thinking in a way of 3 + 2i think about complex numbers as [3, 2]. There is no i expressed directly.
I hope it makes things clearer a little bit
Originally Posted by jason18241
Sorry if I sound like a retard
You don't. At least until you say anythiong like PLEASEE HEEELLLPP!!!11!eleven!! GIVE ME TEH CODEZ!
Re: Problem with Classes and Accessing Private Members
Thanks again for your help. Looks like my initial mistake was forgetting class member functions are private by default - I'll make the constructors public and go from there.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.