Why I cannot cout a string?

C++StringCout

C++ Problem Overview


Why I cannot cout string like this:

string text ;
text = WordList[i].substr(0,20) ;
cout << "String is  : " << text << endl ;

When I do this, I get the following error:

> Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**

It is amazing, that even this is not working:

string text ;
text = "hello"  ;
cout << "String is  : " << text << endl ;

C++ Solutions


Solution 1 - C++

You need to include

#include <string>
#include <iostream>

Solution 2 - C++

You need to reference the cout's namespace std somehow. For instance, insert

using std::cout;
using std::endl;

on top of your function definition, or the file.

Solution 3 - C++

There are several problems with your code:

  1. WordList is not defined anywhere. You should define it before you use it.
  2. You can't just write code outside a function like this. You need to put it in a function.
  3. You need to #include <string> before you can use the string class and iostream before you use cout or endl.
  4. string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.

Solution 4 - C++

Above answers are good but If you do not want to add string include, you can use the following

ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();

return os;
}

Solution 5 - C++

You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.

#include<iostream>
#include<string>
using namespace std;

Solution 6 - C++

Use c_str() to convert the std::string to const char *.

cout << "String is  : " << text.c_str() << endl ;

Solution 7 - C++

If you are using linux system then you need to add

using namespace std;

Below headers

If windows then make sure you put headers correctly #include<iostream.h>

#include<string.h>

Refer this it work perfectly.

#include <iostream>
#include <string>

int main ()
{
std::string str="We think in generalities, but we live in details.";
                                       // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

   std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     
// get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtaView Question on Stackoverflow
Solution 1 - C++Kiril KirovView Answer on Stackoverflow
Solution 2 - C++mike3996View Answer on Stackoverflow
Solution 3 - C++sepp2kView Answer on Stackoverflow
Solution 4 - C++Maheswar ReddyView Answer on Stackoverflow
Solution 5 - C++Ash GhalView Answer on Stackoverflow
Solution 6 - C++Anthony.View Answer on Stackoverflow
Solution 7 - C++pratikpchprView Answer on Stackoverflow