我不明白爲什麼這並不編譯:我可以在C重寫一個字符串返回類型函數++
#include<iostream>
#include<string>
using namespace std;
class Product {
public:
virtual void print() = 0;
virtual void slog() = 0;
virtual void loft() = 0;
};
class Bike: public Product {
private:
string s;
public:
Bike(string x){
s = x;
}
void print() {
std::cout << "Bike";
}
int slog() {
return 4;
}
string loft() {
return s;
}
};
int main() {
string s("Hello");
Product *p = new Bike(s);
p->print(); //works fine
cout << p->slog();//works fine
cout << p->loft(); //error
return 0;
}
- 錯誤上面的代碼的結果。爲什麼我不能重寫字符串類。
- 我想使用指針
p
調用loft()
。 - 有沒有辦法實現這個使用指針對象的抽象類
Product
你不_override_'的std :: string'。請修復你的代碼。請顯示您的代碼中包含的逐字錯誤。 –
請下一次,讓你的代碼更具可讀性。從長遠來看,它會變得更順暢:) –
我會開始看'行30:7:錯誤:虛擬函數'slog'具有與它重寫的函數不同的返回類型('int')返回類型爲'void') int slog(){...'並且自己處理來自頂部的錯誤(通常更有意義,修復第一個錯誤,然後重新運行,因爲「後續錯誤」如果有什麼不可修復的,只需複製上面問題中的剩餘錯誤,看看人們是否可以進一步幫助。謝謝。 – Dilettant