C++ 返回C+中的cout+;使用继承
试图用继承返回一个句子,但一直出现此错误C++ 返回C+中的cout+;使用继承,c++,C++,试图用继承返回一个句子,但一直出现此错误 二进制表达式('ostream'(也称为'basic_ostream')和'void')的操作数无效。 所以我想知道我做错了什么,我以前一直在玩Int,它可以工作,但现在因为它的字符串被弄乱了 #include <iostream> using namespace std; class Knowledge{ public: void getKnowledgeJussi(){ cout<< " coding
二进制表达式('ostream'(也称为'basic_ostream')和'void')的操作数无效。
所以我想知道我做错了什么,我以前一直在玩Int,它可以工作,但现在因为它的字符串被弄乱了
#include <iostream>
using namespace std;
class Knowledge{
public:
void getKnowledgeJussi(){
cout<< " coding exp + " << endl;
}
void getKnowledgeJarno(){
cout<< " talking/communication exp" << endl;
}
};
class Masnad : public Knowledge{
public :
void getMasnadKnowledge(){
return cout << getKnowledgeJussi() << getKnowledgeJarno() << " = " + "Masnad with so much XP" << endl;
}
};
int main() {
Masnad mas;
cout << mas.getMasnadKnowledge() << endl;
}
#包括
使用名称空间std;
课堂知识{
公众:
void getKnowledgeJussi(){
cout由于您返回一个void,cout
将不知道打印什么。我认为您希望返回如下字符串:
string getKnowledgeJussi(){
return ("coding exp + \n");
}
string getKnowledgeJarno(){
return ("talking/communication exp \n");
}
string getMasnadKnowledge(){
return (getKnowledgeJussi() + getKnowledgeJarno() + " = " + "Masnad with so much XP \n");
}
现在您可以执行以下操作:
Masnad mas;
cout << mas.getMasnadKnowledge() << endl;
Masnad-mas;
难道你没有返回任何相关信息,并且期望cout
会理解这一点吗?首先澄清你的接口。你应该返回字符串和字符串连接。正如你所看到的那样,尝试输出void
失败得很惨。另外,使用'\n'
而不是endl
为什么retu上的括号rn str??个人偏好和编码风格。这不重要。@Dam这只是为了组织。这只是我的编码风格,不需要
#include <string>