请注意,我是在一个没有头文件的类文件中编写这个测试代码的,我正转发声明Counter2并尝试调用它上的方法,这会导致编译时错误,无效使用不完整的类型‘Counter2’,有没有解决这个错误的方法?在.cpp文件中是否有forwrd声明的意义,或者它只在头文件中有用。
//TypeConversion.cpp
#include <iostream>
using namespace std;
class Counter2;
class Counter {
public:
int count;
public :
const int* getCount() const{return &(this->count);}
void setCount() {
this->count=10;
}
// Pre-increment
Counter operator++() {
this->count++;
return (*this);
}
//Post increment
Counter& operator++(int){
Counter dummy;
dummy.count=this->count;//(*this).n
this->count++;
return dummy;
}
const Counter& modify() {
this->count=111;
return *this;
}
Counter() {
this->count=0;
//this->array[5]={1,1,2,3,4};
}
/*Counter(Counter2 &counter2) {
this->count= counter2.getA() + counter2.getB();
}*/
istream& operator>>(istream& in) {
in>>this->count;
return in;
}
void operator=(Counter2 &counter2) {
this->count = counter2.getA() + counter2.getB();
}
/* int& operator[](int index){
return this->array[index];
}*/
};
class Counter2 {
private:
int a ,b;
public:
Counter2() {
this->a=10;
this->b=20;
}
Counter2(Counter &c) {
this->a=c.count;
this->b=c.count;
}
int getA() {return this->a;}
int getB() {return this->b;}
operator Counter() {
Counter c;
return c;
}
};
int main() {
cout << "!World!" << endl; // prints !World!
Counter2 c2;
Counter c=c2;
//c=c2;
cout<<endl<<*(c.getCount());
return 0;
}发布于 2016-01-04 20:15:33
在头文件中使用转发声明,例如,能够保存类的对象。但是一旦你想要访问这个类的方法或成员(在你的.cpp中),你就必须在定义中包含正确的头文件。
https://stackoverflow.com/questions/34590922
复制相似问题