[英]C++ multilevel inheritance
我創建了一個程序,其中有三個類,每個類都相互繼承但是當我嘗試創建一個派生類函數時,cout給了我一個錯誤,比如這個
3 IntelliSense: no suitable user-defined conversion from "std::basic_ostream<char, std::char_traits<char>>" to "std::string" exists e:\Visual Studio Projects\test\test\Source.cpp 19 10 test
我想改變什么,解決方案是什么。 如果你可以指出我的錯誤,這將是很好的
#include<iostream>
#include <string>
std::string name;
class Base{
public:
void getRed(){
std::cout << "Your name is : " << name << std::endl;
}
};
class Boy:public Base{
public:
Boy(){
name = "john";
}
std::string newInf(){
return std::cout << "Boy2 name is: " << name << std::endl;
}
};
class Boy2: public Boy{
public:
Boy2(){
name = "Mike";
}
};
int main(){
Boy boy;
boy.getRed();
Boy2 boy2;
boy2.newInf();
system("pause");
return 0;
}
您的編譯錯誤與多級繼承無關。
std::string newInf(){
return std::cout << "Boy2 name is: " << name << std::endl;
}
這是錯的。 std::cout << "Boy2 name is: " << name << std::endl
是,好吧......一種std::basic_ostream &
你不能把它轉換成std::string
。
這應該沒問題,就像你寫了getRed()
。
void newInf(){
std::cout << "Boy2 name is: " << name << std::endl;
}
你將newInf()定義為一個返回std :: string的函數,
你實際上在哪里返回std :: cout ,這是ostream 。
所以在運行時它試圖將ostream轉換為字符串,並失敗,這就是你收到消息的原因:
沒有合適的用戶定義的從“std :: basic_ostream>”到“std :: string”的轉換
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.