C++, Wie die Anzeige/print ein string-Objekt? cout << int funktioniert, cout << string nicht

Ich lief in ein Problem von google nicht lösen konnte. Warum ist, dass cout arbeitet für ein int-Objekt, aber nicht ein string-Objekt in dem folgenden Programm?

#include<iostream>
using namespace std;
class MyClass {
    string val;
public:
    //Normal constructor.
    MyClass(string i) {
        val= i;
        cout << "Inside normal constructor\n";
    }
    //Copy constructor 
    MyClass(const MyClass &o) {
        val = o.val;
        cout << "Inside copy constructor.\n";
    }
    string getval() {return val; }
};
void display(MyClass ob)
{
    cout << ob.getval() << endl;    //works for int but not strings
}
int main()
{
    MyClass a("Hello");
    display(a);
    return 0;
}
  • Was meinst du mit "funktioniert"? Warum lassen Sie nicht getval() const?
  • Für mich funktioniert. Was ist Ihre seen Ausgang und expected Ausgabe. In welcher Weise sollte Sie anders sein.
  • Awesome name btw.
  • Carnegie Dank!
InformationsquelleAutor VSB | 2011-08-05
Schreibe einen Kommentar