problem with private member...

L

Leszek Tumm

Hi.
Can you help solve a problem with operator overloading ?
in one file i have definition of my class and declarations of overloaded
operators:


class Complex
{

long double Re;
long double Im;

public:
friend const Complex operator+ (const Complex &, const Complex &);

//..//
};

Second file contains definition of operator+ :

const Complex operator+ (const Complex & x, const Complex & y)
{
return Complex(x.Re + y.Re, x.Im + y.Im);
}

I don't know why but i'm getting these error messages:


complex.h: In function `const Cmpl::Complex operator+(const Cmpl::Complex&,
const Cmpl::Complex&)':
complex.h:20: `long double Cmpl::Complex::Re' is private
complex.cpp:34: within this context
complex.h:20: `long double Cmpl::Complex::Re' is private
complex.cpp:34: within this context
complex.h:21: `long double Cmpl::Complex::Im' is private
complex.cpp:34: within this context
complex.h:21: `long double Cmpl::Complex::Im' is private
complex.cpp:34: within this context

line 34 is:
return Complex(x.Re + y.Re, x.Im + y.Im);


What is the reason of this error? What should I do to make it
compile properly??

Thanks.

Leszek
 
J

James Rafter

Post the exact code. I'll guess that the compiler
doesn't know your function definition is the one
that has been declared friend because it's not in
the same namespace as the class declaration.
 
E

emofine

It's hard to say for sure, because you have omitted what
compiler/version you are using (looks like some version of g++), and
the fact that you have declared Complex in a namespace named Cmpl. The
compiler version is important because if you are using an older C++
compiler, there are new rules in terms of namespaces and
argument-dependent (Koenig) lookup (ADL). Some C++ compilers implement
ADL but do not use it by default without special command-line flags
(e.g. HPUX aCC).

Are you sure you have declared the operator+ function in the same
(Cmpl) namespace? If not, the compiler won't find it and you will get
the error message you reported. To fix the error, ensure that the
operator+() is implemented in the same namespace as the class:

namespace Cmpl {
class Complex {...};

const Complex operator+ (const Complex & x, const Complex & y)
{
return Complex(x.Re + y.Re, x.Im + y.Im);
}

}

This worked on my g++ compiler (3.3.1).
 
E

E. Robert Tisdale

Leszek said:
Can you help solve a problem with operator overloading?
In one file, I have definition of my class and declarations of overloaded
I have definition of my class and declarations of overloaded operators:
cat Complex.h
#ifndef GUARD_COMPLEX_H
#define GUARD_COMPLEX_H 1

namespace cmpl {
class Complex {
private:
// representation
long double Re;
long double Im;
public:
// operators
friend const Complex operator+(
const Complex&, const Complex&);
// constructors
Complex(const long double& re, const long double& im):
Re(re), Im(im) { }
};
} // namespace cmpl

#endif//GUARD_COMPLEX_H
cat Complex.cc
#include <Complex.h>

namespace cmpl {
const Complex operator+(const Complex& x, const Complex& y) {
return Complex(x.Re + y.Re, x.Im + y.Im);
}
}
g++ -I. -Wall -ansi -pedantic -c Complex.cc

It works just fine for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top