首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转发声明cpp无效使用不完整的类型

转发声明cpp无效使用不完整的类型
EN

Stack Overflow用户
提问于 2016-01-04 20:14:09
回答 1查看 1.9K关注 0票数 0

请注意,我是在一个没有头文件的类文件中编写这个测试代码的,我正转发声明Counter2并尝试调用它上的方法,这会导致编译时错误,无效使用不完整的类型‘Counter2’,有没有解决这个错误的方法?在.cpp文件中是否有forwrd声明的意义,或者它只在头文件中有用。

代码语言:javascript
复制
//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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-01-04 20:15:33

在头文件中使用转发声明,例如,能够保存类的对象。但是一旦你想要访问这个类的方法或成员(在你的.cpp中),你就必须在定义中包含正确的头文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34590922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档