151

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 ;
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
Ata
  • 12,126
  • 19
  • 63
  • 97

7 Answers7

255

You need to include

#include <string>
#include <iostream>
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • 7
    and also `using namespace std` or `using std::cout`; `using std::endl`; – fardjad Jun 12 '11 at 08:44
  • 2
    Yes, but I guess it's included, as there's no error on `string text;` also the edit (added error) says, that this is not the problem but the missing `string` header. – Kiril Kirov Jun 12 '11 at 08:46
  • 58
    +1: Many STL headers in Visual C++ (including ) pull in a definition of the `std::basic_string` class (because they indirectly include the implementation-defined header (*never include that directly*)). While that allows you to use the string class, the relevant `operator<<` is defined in the header itself, so you must include that manually. Also relying on other headers to indirectly include the definition of `std::basic_string` works in VC++, but it won't work on all compilers. – Sven Jun 12 '11 at 08:59
  • 6
    Sven- Your comment is awesome! I had a similar problem as this questioner, compiler said operator >> was not defined for types std::cin and std::string. It turns out I had but had forgetten . I'm used to working on linux w/ gcc which would have complained that std::string is not defined. Your comment explains perfectly why we instead got the complaint about the operator. Thanks!! – Daniel Goldfarb Jan 27 '13 at 17:09
  • @PreetamSingh - uhmmmm, what? – Kiril Kirov Oct 19 '13 at 16:17
  • 2
    This works. I missed the #include line in my code. Thanks. – Hao Nguyen Oct 30 '14 at 22:21
  • 1
    Order of both #includes is important in some C++ environments (because of conditional compilation) – Jacek Cz Jun 18 '16 at 15:13
11

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.

mike3996
  • 17,047
  • 9
  • 64
  • 80
7

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.
sepp2k
  • 363,768
  • 54
  • 674
  • 675
1

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

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;
dev-masih
  • 4,188
  • 3
  • 33
  • 55
Ash Ghal
  • 33
  • 1
0

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

cout << "String is  : " << text.c_str() << endl ;
Anthony.
  • 643
  • 7
  • 14
-3

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;
}
pratikpchpr
  • 419
  • 5
  • 4
  • `using namespace std;` has nothing to do with the target os being `linux`. Similarly, adding the `.h` to the includes has nothing to do with the target os being windows, `#include ` and `#include ` will work on windows. – StaticBeagle May 26 '17 at 02:10