Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I am using Vstudio 2003 .NET in learning c++ in cli
I know when ever I start a new project my code comes up as
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}


But when I start it I want it to come up prepared like

C#
#include "stdafx.h"
#include <conio.h>
using namespace std

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}


[EDIT]
cause when I compile with just stdafx, and without std nsamespace, i get

Collapse

C++
// cout.cpp : Defines the entry point for the console application.
//

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

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


Collapse

error C2065: 'endl' : undeclared identifier
error C2065: 'cout' : undeclared identifier
Posted
Updated 11-Apr-11 9:33am
v3

Herboren wrote:
#include "stdafx.h" #include <conio.h>
using namespace std

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


You forgot a semicolon after std.
BTW What is the purpose of the using directive if you haven't any std header included?
:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-11 21:33pm    
Correct, my 5.
--SA
Herboren 11-Apr-11 18:53pm    
^ Posted 5 Mar '10
Sergey Alexandrovich Kryukov 11-Apr-11 22:27pm    
Yes, that's funny. By some reason, come across it now.
--SA
Albert Holguin 11-Apr-11 23:00pm    
details, details... good catch cpallini... :)
While this doesn't answer your question, you do not need to add using namespace for every namespace you invoke. In fact, by doing so you're undermining the very reason why these namespaces have been introduced in the first place: avoiding the cluttering of the global namespace.

Without the using statement you can still access individual symbols just by adding the name of the namespace as a prefix like this:
std::cout << "hello" << std::endl;


You can even inject singular objects or types from other namespaces into your own if you use them often:
#include <io>
typedef std::ostream ostream; // creates 'ostream' as a symbol in global namespace
ostream& cout(std::cout); // creates cout as a reference to std::cout

void hello() {
    cout << "hello" << std::endl; // uses ::cout defined above
}


I'm not saying you shouldn't take advantage of the using statement ever, I just think it's a bad idea to manipulate your IDE into adding it into every project!

Besides, adding this into your main.cpp file doesn't help for other cpp files. If you really want this to affect all files in your project, add it to stdafx.h instead.
 
Share this answer
 
Answering follow-up Question (after [EDIT]):

Using cout required std and iostream header:

#include <iostream>
using namespace std;

//...</iostream>


—SA
 
Share this answer
 
You could try to modify
the root.cpp file in your VC\..\templates\.. directory :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900