Why I can't use "cout" in Visual C++

I've written the following code using Visual C++, Console Based application for Win32.

The code gives "Compiler Error C2065  'identifier' : undeclared identifier".
What could be the reson for this to happen?


#include <iostream.h>
#include <string.h>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
      char *s="Hello world";
      cout<<strstr(s, "wo")<<endl;

      string s( "TEST" );      

      return 0;
}

csoundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
AxterCommented:
Hi csound,
> #include <iostream.h>

<iostream.h> is not part of the C++ standard, and therefore not portable to modern compilers like VC++ 7.x or 8.x.

Use <iostream> instead, which is part of the C++ standard, and therefore portable.
If you don't want to fully qualify the name, you'll also want to add using clause.
Example:

#include <iostream>

using namespace std;

David Maisonave (Axter)
Cheers!
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
AxterCommented:
Full Example:

#include <iostream>
#include <string>
#include "stdafx.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
     char *s="Hello world";
     cout<<strstr(s, "wo")<<endl;

     string s( "TEST" );      

     return 0;
}

0
 
AxterCommented:
The <string.h> header is for the C string functions, and not for std::string.
If you want to use std::string, you need <string> header instead, as in the above example.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
choo_chuCommented:
Hi,

If you are using precompiled headers, #include "stdafx.h" should be the first #include:

#include "stdafx.h"
#include <iostream>
#include <string>
          :
          :


choo_chu
0
 
patti_nylCommented:
you can write like this

std::cout << strstr(s, "wo") << std::endl;
0
 
csoundAuthor Commented:
Thank you for your helps!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.