Why I cannot cout a string?
Asked Answered
C

7

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 ;
Crusty answered 12/6, 2011 at 8:40 Comment(9)
Can you edit in the error message?Prejudicial
Did you #include <iostream> ?Ferree
not enough info. what is the errorRideout
I have did that . but again , I have problem .Crusty
Can you post the entire file? We don't know if you're calling this in a function, if you included the right things, etc...Ferree
you are missing includesTripersonal
There's nothing "amazing" in this, it's even expected..Did you try my answer?Spleen
Post the entire code, not just a part of it.Nievesniflheim
So this was just a compilation error?Excitor
S
256

You need to include

#include <string>
#include <iostream>
Spleen answered 12/6, 2011 at 8:42 Comment(7)
and also using namespace std or using std::cout; using std::endl;Dustproof
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.Spleen
+1: Many STL headers in Visual C++ (including <iostream>) pull in a definition of the std::basic_string class (because they indirectly include the implementation-defined <xstring> header (never include that directly)). While that allows you to use the string class, the relevant operator<< is defined in the <string> 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.Mentality
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 <iostream> but had forgetten <string>. 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!!Foxing
@PreetamSingh - uhmmmm, what?Spleen
This works. I missed the #include <string> line in my code. Thanks.Metralgia
Order of both #includes is important in some C++ environments (because of conditional compilation)Paperweight
B
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.

Bloodshed answered 12/6, 2011 at 8:45 Comment(0)
G
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.
Ginoginsberg answered 12/6, 2011 at 8:46 Comment(1)
none of them worked for me , it seem that problem is with substrCrusty
G
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;
}
Greenock answered 6/5, 2018 at 11:16 Comment(0)
A
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;
Abra answered 10/9, 2013 at 0:34 Comment(1)
Welcome to StackOverflow, you may not have noticed but this was addressed in one of the comments of the accepted answer.Lourdeslourie
S
0

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

cout << "String is  : " << text.c_str() << endl ;
Substandard answered 9/2, 2020 at 4:2 Comment(0)
V
-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;
}
Valona answered 26/5, 2017 at 1:10 Comment(1)
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 <iostream> and #include <string> will work on windows.Solicitude

© 2022 - 2024 — McMap. All rights reserved.