c++ - Invalid use of type class after forward declaration -


i have written program demonstrate double dispatch in c++. shows invalid use of type class though have forward declared classes. there way rectify without writing in separate header files.

#include <iostream> #include <string> using namespace std;  class number;        class integer; class rational;  class number{     public:         int num, den;         virtual void add(number&) = 0;         virtual void addinteger(integer&) = 0;         virtual void addrational(rational&) = 0;         virtual string tostring() = 0; };    class rational : public number{         void addinteger(integer& i){             this->num = this->num + i.num*this->den;             this->den = this->den;             cout << this->num << "/" << this->den;         }          void addrational(rational &r){             this->num = this->num*r.den + this->den*r.num;             this->den = this->den*r.den;             cout << this->num << "/" << this->den;          }      public:         void add(number& n){             n.addrational(*this);         }          rational(int n, int d){             this->num = n;             this->den = d;         } };   class integer : public number{         void addinteger(integer& i){                 this->num += i.num;                 this->den = 1;                 cout << this->num;         }          void addrational(rational& r){             this->num = this->num*r.den + r.num;             this->den = r.den;             cout << "this->num" << "/" << this->den;         }      public:          void add(number& n){             n.addinteger(*this);         }          integer(int n){             this->num = n;             this->den = 1;         } };    int main(){     cout << "helo world";     return 0; } 

you need separate function declarations definitions. function definitions need appear after class definitions use.

example code

#include <iostream> #include <string> using namespace std;  class number;        class integer; class rational;  class number { public:     int num, den;     virtual void add(number&) = 0;     virtual void addinteger(integer&) = 0;     virtual void addrational(rational&) = 0;     virtual string tostring() = 0; };  class rational : public number {     void addinteger(integer& i);      void addrational(rational &r);  public:     void add(number& n)     {         n.addrational(*this);     }      rational(int n, int d)     {         this->num = n;         this->den = d;     } };  class integer : public number {     void addinteger(integer& i);      void addrational(rational& r);  public:     void add(number& n)     {         n.addinteger(*this);     }      integer(int n)     {         this->num = n;         this->den = 1;     } };  void rational::addinteger(integer& i) {     this->num = this->num + i.num*this->den;     this->den = this->den;     cout << this->num << "/" << this->den; }  void rational::addrational(rational &r) {     this->num = this->num*r.den + this->den*r.num;     this->den = this->den*r.den;     cout << this->num << "/" << this->den;  }  void integer::addinteger(integer& i) {     this->num += i.num;     this->den = 1;     cout << this->num; }  void integer::addrational(rational& r) {     this->num = this->num*r.den + r.num;     this->den = r.den;     cout << "this->num" << "/" << this->den; }  int main() {     cout << "helo world";     return 0; } 

live example


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -