Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now