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

Invalid use of type class after forward declaration

本文关键字:无效 类型 声明      更新时间:2023-10-16

我写了这个程序来演示C++中的双重调度。但它显示类型类的使用无效,即使我已经向前声明了我的类。有没有办法在不写入单独的头文件的情况下纠正此问题。

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

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

示例代码

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

现场示例