CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2008
    Posts
    3

    Exclamation 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.

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    class Complex {
    	Complex(double realValue, double imaginaryValue);
    	Complex(double realPart);
    	Complex();
    public:
    	double getReal();
    	double getImaginary();
    private:
    	double real, imaginary;
    };
    
    int main() {
    	/*
       Complex one, two(3.2), x(2.7, 18.2), y(15.3,9.72);
       cout << x << " + " << y << " = " << (x + y) << endl;
       cout << x << " - " << y << " = " << (x - y) << endl;
       cout << x << " * " << y << " = " << (x * y) << endl;
       cout << ((one == (two - two))? "Equal" : "Not equal") << endl;
       cout << ((x == y) ? "Same" : "Not same") << endl;
       return 0;
       */
    }
    
    bool operator == (Complex value1, Complex value2) {
    	return ( ( value1.getReal()==value2.getReal() ) && ( value1.getImaginary()==value2.getImaginary() ) );
    }
    
    Complex operator + (Complex value1, Complex value2) {
    	double totalReal = value1.getReal()+value2.getReal();
    	double totalImaginary = value1.getImaginary()+value2.getImaginary();
    	return Complex(totalReal, totalImaginary); //error
    }
    
    Complex operator - (Complex value1, Complex value2) {
    	double totalReal = value1.getReal()-value2.getReal();
    	double totalImaginary = value1.getImaginary()-value2.getImaginary();
    	return Complex(totalReal, totalImaginary); //error
    }
    
    Complex operator * (Complex value1, Complex value2) {
    	double a = value1.getReal();
    	double b = value1.getImaginary();
    	double c = value2.getReal();
    	double d = value2.getimaginary();
    	return ( (a*c-b*d)+(a*d+b*c)*pow(-1, .5) );
    
    }
    
    double Complex::getReal() {
    	return real;
    }
    
    double Complex::getImaginary() {
    	return imaginary;
    }
    
    Complex::Complex(double realValue, double imaginaryValue) {
    	real = realValue;
    	imaginary = imaginary*pow(-1, .5);
    }
    
    Complex::Complex(double realPart) {
    	real = realPart;
    	imaginary = 0*pow(-1, .5);
    }
    
    Complex::Complex() {
    	real = 0;
    	imaginary = 0*pow(-1, .5);
    }
    Thanks,
    -Jason
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Nov 2008
    Posts
    3

    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.

    -Jason

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Problem with Classes and Accessing Private Members

    Quote 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.

    Quote 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.

    Quote 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).


    Quote 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

    Quote 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!


    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Nov 2008
    Posts
    3

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured