CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  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

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