Operator overloading definition outside of the class - C++ -


sorry such simple question. i've been using tutorialspoint.com learn c++. i'm trying learn classes , operator overloading. other member function, website uses convention

    class box {      private:       int volume;      public:       box(int num);       ...      }      box::box(int num) {       volume = num;      } 

however, when overloading operators, use this

     class box {       private:        int volume;       public:        box(int num);        box operator+(const box &b) {         box box;         box.volume = this->volume + b.volume;         return box;        }       } 

they define overloading function inside class. possible define outside class? if so, how? i've tried

     box box::operator+(const box &b) {...}      box::box operator+(const box &b) {...} 

but these don't work

how do outside of class?

again, sorry such simple question. thanks

edit code looks this

#include <iostream> #include <string> using namespace std;  class box {  private:   int volume;  public:   box(int num);   box operator+(const box &b); };  box::box(int num) {  volume = num; }  box box::operator+(const box &b) {  box box;  box.volume = this->volume + b.volume;  return box; }  int main() {  box one(2);  box two;  2 = 1 + one;   } 

my error is

    overloading.cc: in member function 'box box::operator+(const box&)':     overloading.cc:18:6: error: no matching function call 'box::box()'     overloading.cc:18:6: note: candidates are:     overloading.cc:13:1: note: box::box(int)     overloading.cc:13:1: note:   candidate expects 1 argument, 0 provided     overloading.cc:5:7: note: box::box(const box&)     overloading.cc:5:7: note:   candidate expects 1 argument, 0 provided     overloading.cc: in function 'int main()':     overloading.cc:25:6: error: no matching function call 'box::box()'     overloading.cc:25:6: note: candidates are:     overloading.cc:13:1: note: box::box(int)     overloading.cc:13:1: note:   candidate expects 1 argument, 0 provided     overloading.cc:5:7: note: box::box(const box&)     overloading.cc:5:7: note:   candidate expects 1 argument, 0 provided 

yes, possible, , common, define operators outside of class whenever possible. want this:

box operator+( const box& left, const box& right ) { ... } 

edit:

if mean want declare operator member function , define elsewhere (in cpp) is:

h

class box {     box operator+( const box& other ) const; }; 

cpp

box box::operator+( const box& other ) const { ... } 

edit 2: you've posted error. attempting use default constructor when create box two; however, since created non-default constructor box( int ) compiler not generating default constructor you.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -