我编写了一个程序,其中包含三个类,每个类都相互继承,但是当我尝试创建派生类函数时,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;
}
关于C++多级继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21635813/