[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-05
7
Medium Priority
?
6,904 Views
Last Modified: 2012-06-21
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;
}

0
Comment
Question by:csound
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 320 total points
ID: 16838398
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
 
LVL 30

Expert Comment

by:Axter
ID: 16838402
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
 
LVL 30

Expert Comment

by:Axter
ID: 16838407
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Assisted Solution

by:choo_chu
choo_chu earned 80 total points
ID: 16876777
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
 

Expert Comment

by:patti_nyl
ID: 16992111
you can write like this

std::cout << strstr(s, "wo") << std::endl;
0
 

Author Comment

by:csound
ID: 17159188
Thank you for your helps!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question