Masnad Nihit Masnad Nihit - 1 month ago 19
C++ Question

returning cout in C++ using inheritance

Trying to return a sentence out with inheritance but keep getting this error

invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'void')


So wanted to know what I am doing wrong, I was playing around with Int before and it was working but now since its string its all messed up.

#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;
}

Answer

Since you return a void cout wont know what to print. I think you want to return a string like so:

string getKnowledgeJussi(){
   return ("coding exp + \n");
}
string getKnowledgeJarno(){
   return ("talking/communication exp \n");
}
string getMasnadKnowledge(){
   return  (getKnowledgeJussi()  + getKnowledgeJarno() +  " = " + "Masnad with   so much XP \n");
}

Now you can do so:

 Masnad mas;
 cout << mas.getMasnadKnowledge() << endl;

Note : dont forget to include the string library:

#include <string>
Comments