Domanda

I can use cout to print a normal variable just fine, but whenever I try to print a function variable(in this case, string input) the compiler shoots out the error:

C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

I have posted my code below. Is there anything that I am doing wrong?(I am using the C++ version of Eclipse)

Here's my code so far:

#include <iostream>
using namespace std;

void println(string text) {
    cout << text << endl;
}

int main() {
    int test = 5;
    cout << test;
    return 0;
}
È stato utile?

Soluzione

If you want to use std::string, you need to include <string>! The type may be declared or even defined because it is used by the IOStream classes but the implementation may choose not to include the entire header. In your code it seems you got a definition of std::string but not the declaration of its output operator.

Altri suggerimenti

Operator << for class std::basic_string are declared in header <string>. So you need to include it in your program that the compiler will know about it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top