Solved

Why does Visual Studio seem to ignore my include file directives?

Posted on 2013-06-20
2
394 Views
Last Modified: 2013-06-20
I'm getting an error in Visual Studio on something that should be very simple.

In the following code, cout is flagged as undefined when I attempt to compile.

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

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Hello World\n";
	return 0;
}

Open in new window


I tried <iostream.h>, I tried putting the include in stdafx.h, all to no avail.  The compiler keeps telling me that cout is undefined; if I press f12 while selecting cout, it tells me the symbol is defined in iostream.h.  But intellisense says cout is undefined and the compiler gives an error indicating cout is undefined.

This is inside a WIN32 Console application. The source code above is the complete contents of test.cpp.

The error message is:
Error      2      error C2065: 'cout' : undeclared identifier
0
Comment
Question by:Ryan94114
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39264624
The reason is two-fold:

a) 'StdAfx.h' has to be the very first file to be included
b) you are using 'cout' without adding the std namespace to your code

So, there are basically two ways to do that:

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

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Hello World\n";
	return 0;
}
                                  

Open in new window


or

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Hello World\n";
	return 0;
}
                                  

Open in new window

0
 
LVL 14

Expert Comment

by:brendanmeyer
ID: 39264631
either add: just below the includes
using namespace std;

Open in new window


or on ever occurrence of cout use:
std::cout

Open in new window

0

Featured Post

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.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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