 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Leszek Tumm Guest
|
Posted: Wed Dec 22, 2004 8:53 am Post subject: problem with private member... |
|
|
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Dec 23, 2004 3:30 pm Post subject: Re: problem with private member... |
|
|
Leszek Tumm wrote:
| Quote: | 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??
|
The error message says it (if you read very carefully): notice
the 'Cmpl::' prefix on the class name, but not on operator+.
You probably wanted to say something like:
friend Complex ::operator+(....)
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Thu Dec 23, 2004 5:07 pm Post subject: Re: problem with private member... |
|
|
Your constructor for Complex is missing :)
--lsu
class Complex
{
long double Re;
long double Im;
friend const Complex operator+ (const Complex &, const Complex &);
public:
Complex(double re, double im) : Re(re), Im(im) {}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Thu Dec 23, 2004 9:10 pm Post subject: Re: problem with private member... |
|
|
"Leszek Tumm" <leszekt80 (AT) poczta (DOT) onet.pl> skrev i meddelandet
news:pan.2004.12.22.03.36.17.562076 (AT) poczta (DOT) onet.pl...
| Quote: | 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??
|
Is the operator+() defined within the namespace Cmpl?
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paavo Helde Guest
|
Posted: Thu Dec 23, 2004 9:12 pm Post subject: Re: problem with private member... |
|
|
"Leszek Tumm" <leszekt80 (AT) poczta (DOT) onet.pl> wrote in
news:pan.2004.12.22.03.36.17.562076 (AT) poczta (DOT) onet.pl:
| Quote: | 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
|
My crystal ball says that you have encapsulated your .h file in namespace
Cmpl {...}, but only said 'using namespace Cmpl;' in the .cpp file.
Therefore the operator+ defined there is considered to be in the global
namespace and not the one you declared a friend. Try:
const Complex Cmpl::operator+ (const Complex & x, const Complex & y)
{
return Complex(x.Re + y.Re, x.Im + y.Im);
}
or encapsulate the .cpp also in the namespace (most suggested if you use
free functions).
Regards
Paavo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|