任務是創建類,該類在每個時刻對其類型的對象進行計數。這是我的代碼。錯誤是: 1.對象具有與成員函數「counter :: print」不兼容的類型限定符; 2.返回值類型與函數類型不匹配; - 這真是太棒了! 我糾正了錯誤,它給了我一個我無法修復的新錯誤; 1. '無效計數器::打印(無效)':不能轉換從 'const的計數器' '這個' 指針 '計數器&'在複製構造函數C++中使用print()函數
class counter {
private:
static int count;
public:
counter();
counter(const counter &from);
void print() const;
~counter();
};
counter::counter() {
++count;
}
counter::counter(const counter &from) {
++count;
cout << "Copy constructor:\t";
from.print(); // here is the error
}
void counter::print() const{
cout << "\t Number of objects = " << count << endl;
}
counter::~counter() {
--count;
cout << "Destructor:\t\t";
print();
}
int counter::count = 0;
counter f(counter x);
void main() {
counter c;
cout << "After constructing of c:";
c.print();
cout << "Calling f()" << endl;
f(c);
cout << "After calling f():";
c.print();
}
counter f(counter x) {
cout << "Argument inside f():\t";
x.print();
return x;
}
'return x;'as'void'? – LogicStuff
所以你已經修復了下面指出的錯誤。當你說'void counter :: print(void)'時:不能將'this'指針從'const counter'轉換爲'counter&'正確* where * this does not say this> –