前向声明后无效使用类型类

时间:2016-02-05 14:26:07

标签: c++ class oop

我编写了这个程序来演示C ++中的Double dispatch。但是它显示了类型类的无效使用,即使我已经向前声明了我的类。有没有办法纠正这个问题,而无需在单独的头文件中写入。

#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;
}

1 个答案:

答案 0 :(得分:1)

您只需要将函数声明与其定义分开。函数定义需要在它们使用的类定义之后出现。

示例代码

#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