Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error in reload operator

Error in reload operator

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chen XuNuo

    #include #include #include using namespace std; class Complex{ public: Complex(); Complex(double); Complex(double,double); void write() const ; bool operator==(const Complex& ) const ; bool operator!=(const Complex& ) const ; Complex operator-(); friend ostream& operator<<(ostream& out,Complex& c); private: double real; double imag; }; Complex::Complex(){ real=imag=0.0; } Complex::Complex(double re){ real=re; imag=0.0; } Complex::Complex(double re,double im){ real=re; imag=im; } void Complex::write() const { cout<

    R Offline
    R Offline
    Rajkumar R
    wrote on last edited by
    #2

    Hi, Your function declaration has different prototype, one has const reference other is not const. Best Regards Raj

    C 1 Reply Last reply
    0
    • C Chen XuNuo

      #include #include #include using namespace std; class Complex{ public: Complex(); Complex(double); Complex(double,double); void write() const ; bool operator==(const Complex& ) const ; bool operator!=(const Complex& ) const ; Complex operator-(); friend ostream& operator<<(ostream& out,Complex& c); private: double real; double imag; }; Complex::Complex(){ real=imag=0.0; } Complex::Complex(double re){ real=re; imag=0.0; } Complex::Complex(double re,double im){ real=re; imag=im; } void Complex::write() const { cout<

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #3

      Because-because-because you have two different operator << overloads; friend ostream& operator<<(ostream& out, Complex& c); ostream& operator<<(ostream& out, const Complex& c) { ... } Their prototypes are different due to the difference inside their parameter list; in first case; Complex & in second case; const Complex & So make them the same; put const in the friend declaration.

      -- ===== Arman

      C 1 Reply Last reply
      0
      • R Rajkumar R

        Hi, Your function declaration has different prototype, one has const reference other is not const. Best Regards Raj

        C Offline
        C Offline
        Chen XuNuo
        wrote on last edited by
        #4

        But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~

        A R 2 Replies Last reply
        0
        • C Chen XuNuo

          But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~

          A Offline
          A Offline
          Arman S
          wrote on last edited by
          #5

          It should work. What's the concrete message of the error?

          -- ===== Arman

          1 Reply Last reply
          0
          • C Chen XuNuo

            But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~

            R Offline
            R Offline
            Rajkumar R
            wrote on last edited by
            #6

            No, It should work for the << operator but shows error for Complex Complex::operator-(){ real=-real; imag=-imag; } because it doesnot return value.

            C 1 Reply Last reply
            0
            • R Rajkumar R

              No, It should work for the << operator but shows error for Complex Complex::operator-(){ real=-real; imag=-imag; } because it doesnot return value.

              C Offline
              C Offline
              Chen XuNuo
              wrote on last edited by
              #7

              NO~after I delete the funtion Complex::operator-() from the class,the error is the same as before~

              R 1 Reply Last reply
              0
              • A Arman S

                Because-because-because you have two different operator << overloads; friend ostream& operator<<(ostream& out, Complex& c); ostream& operator<<(ostream& out, const Complex& c) { ... } Their prototypes are different due to the difference inside their parameter list; in first case; Complex & in second case; const Complex & So make them the same; put const in the friend declaration.

                -- ===== Arman

                C Offline
                C Offline
                Chen XuNuo
                wrote on last edited by
                #8

                NO~no~noI have modified this problembut it do noting to the error~

                D 1 Reply Last reply
                0
                • C Chen XuNuo

                  NO~after I delete the funtion Complex::operator-() from the class,the error is the same as before~

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #9

                  Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message

                  C 2 Replies Last reply
                  0
                  • R Rajkumar R

                    Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message

                    C Offline
                    C Offline
                    Chen XuNuo
                    wrote on last edited by
                    #10

                    there are four: error C2248: 'real' : cannot access private member declared in class 'Complex' error C2248: 'imag' : cannot access private member declared in class 'Complex' error C2593: 'operator <<' is ambiguous error C2593: 'operator <<' is ambiguous

                    1 Reply Last reply
                    0
                    • R Rajkumar R

                      Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message

                      C Offline
                      C Offline
                      Chen XuNuo
                      wrote on last edited by
                      #11

                      By the way,the two previous errors point to the location "return out<

                      1 Reply Last reply
                      0
                      • C Chen XuNuo

                        NO~no~noI have modified this problembut it do noting to the error~

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        Chen-XuNuo wrote:

                        I have modified this problem...

                        To what? By making both the declaration and the definition the same, the compiler error (C2248) will go away.


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups