Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The #include<iostream> exists, but I get an error: identifier "cout" is undefined. Why?

I learn C++ and COM through the books. In the IDE MS Visual Studio 2012 I have created new empty C++ project, and added some existing files to it. My CPP file contains #include<iostream> row, but in editor I got such messages:

Error: identifier "cout" is undefined

end

Error: identifier "endl" is undefined

Code:

#include<iostream> #include"interfaces.h" // unknown.h, objbase.h, initguid.h  class CA {//: public IX, IY{ public:     // Constructor     CA();     // Destructor     ~CA();     // IUnknown     virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv);     virtual ULONG __stdcall AddRef();     virtual ULONG __stdcall Release();     // IX     virtual void __stdcall Fx1();     virtual void __stdcall Fx2();     // IY     virtual void __stdcall Fy1(){ cout << "Fy1" << endl; }  // errors here     virtual void __stdcall Fy2(){ cout << "Fy2" << endl; }  // errors here also private:     long counter; }; 

Why it happens?

like image 583
Andrey Bushman Avatar asked Nov 03 '12 11:11

Andrey Bushman


1 Answers

You need to specify the std:: namespace:

std::cout << .... << std::endl;; 

Alternatively, you can use a using directive:

using std::cout; using std::endl;  cout << .... << endl; 

I should add that you should avoid these using directives in headers, since code including these will also have the symbols brought into the global namespace. Restrict using directives to small scopes, for example

#include <iostream>  inline void foo() {   using std::cout;   using std::endl;   cout << "Hello world" << endl; } 

Here, the using directive only applies to the scope of foo().

like image 69
juanchopanza Avatar answered Sep 24 '22 03:09

juanchopanza