473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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




Jul 22 '05 #1
3 2194
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.

Jul 22 '05 #2
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).

Jul 22 '05 #3
Leszek Tumm wrote:
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: jbieger | last post by:
On my site I want to make two classes. One that can be instantiated normally and one with extended functionality that can only be instantiated once. The Singleton pattern thus seems like a logical...
3
by: Billy Jacobs | last post by:
I have created a DataGridColumnDatePicker Component so that I can put a datetimepicker control in my datagrid. It almost works. When I put my mouse in the cell it changes to a datetimepicker...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
4
by: MrTang001 | last post by:
How I can fix this problem? I don't know why it alway prompted (first use this function). I have use newDollars, newCents... to access the base class member variable. And the member functions for...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
3
by: Thubaiti | last post by:
hi there.. I have a problem with this code... any help please ? SQL: CREATE TABLE member (sn NUMBER(6), nickname VARCHAR2(30), password VARCHAR2(30));
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
2
by: snowie | last post by:
I have a simillar problem to what others have had before me. I just can't find the solution in any earlier post here at the scripts (or any other forum). I am calling a web service that returns a...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.