前向声明后类型类的使用无效
Invalid use of type class after forward declaration
我写了这个程序来演示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;
}
现场示例
相关文章:
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "错误:数组下标的无效类型'float [10001][float]'是什么意思?
- C++ 从两类中确定无效类型*
- 数组下标编译的无效类型'int[int]'由于 -Wfatal 错误而终止
- 数组下标的无效类型INT [INT]
- 此背包代码显示浮点[float]无效类型错误.可能是什么原因
- 我收到"无效类型 int[int]...",但这毫无意义
- 数组下标的无效类型"<未解析的重载函数类型>[int]" - C++
- 一元*的类型冲突error和无效类型参数具有int
- 映射迭代器:一元'*'的无效类型参数(具有"int")
- 一元 '*' 的 #define 和无效类型参数(具有 'double')
- 使用矢量的数组下标'int[int]'无效类型
- 数组相关错误 - 无效类型.
- 未知语法无效(类型::m_function)()
- 如何修复"'一元*'的无效类型参数?
- 高斯-赛德尔函数中的无效类型
- 数组下标和其他无效类型"int[int]"
- 我面临这个:我的程序中数组下标的无效类型"double[int]"