0
請注意,我寫這個測試代碼在一個單一的TypeConversion.cpp文件中沒有頭文件我正在聲明Counter2並試圖調用它的方法counter2.getA();這是給我編譯時錯誤使用不完整的類'class Counter2'有什麼方法可以解決這個錯誤? .cpp文件中是否有任何forwrd聲明或僅在頭文件中有用。轉發聲明CPP無效使用不完整的類型
//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 << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Counter2 c2;
Counter c=c2;
//c=c2;
cout<<endl<<*(c.getCount());
return 0;
}
感謝您的解釋!我能做些什麼來解決上面指定的代碼中的錯誤? – user2206366
轉發聲明也可以在源文件中使用(但在那裏很少用到)。而且你將無法擁有不完整課堂的對象。您需要完整的定義,就像調用成員函數一樣。用不完整的類可以做什麼,是聲明指針和對它們的引用。 – user2079303
@ user2206366:在你的情況下,你必須在'Counter2'定義的類後面定義'void Counter :: operator =(Counter2&counter2)'。 – Jarod42