Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-20
2
Medium Priority
?
402 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

636 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