problem with private member...

Discussion in 'C++' started by Leszek Tumm, Dec 22, 2004.

  1. Leszek Tumm

    Leszek Tumm Guest

    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
     
    Leszek Tumm, Dec 22, 2004
    #1
    1. Advertising

  2. Leszek Tumm

    James Rafter Guest

    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.
     
    James Rafter, Dec 22, 2004
    #2
    1. Advertising

  3. Leszek Tumm

    Guest

    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).
     
    , Dec 22, 2004
    #3
  4. 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.
     
    E. Robert Tisdale, Dec 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    818
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    443
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    545
    Steven Bethard
    Jan 24, 2005
  4. Peng Yu
    Replies:
    3
    Views:
    1,107
    Simon Forman
    Sep 21, 2009
  5. ittium
    Replies:
    5
    Views:
    457
    88888 Dihedral
    Jan 12, 2012
Loading...

Share This Page