Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot cout a string?

Tags:

c++

string

cout

People also ask

Can you cout a string?

To rectify this problem, the standard namespace (abbreviated: std) was created to store objects like cout to be used for their desired purpose. As a result, developers can now use an object like cout to print a string but must point to it in the standard namespace.

Why Cout is not working?

There is no std::cout in C. In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.

Why is cout so slow?

As for why it is so "time consuming", (in other words, slow,) that's because the primary purpose of std::cout (and ultimately the operating system's standard output stream) is versatility, not performance.


You need to include

#include <string>
#include <iostream>

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.


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.

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