Solved

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

Posted on 2013-06-20
2
389 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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