Jump to content
  • Advertisement
Sign in to follow this  
Danoo2007

Nothing will work....

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Nothing i try to compile/build will ever work out... I follow the guide/books and such... Compiling... Hello.cpp c:\users\daniel_administrator\documents\visual studio 2005\projects\newproject\newproject\hello.cpp(7) : error C2065: 'cout' : undeclared identifier c:\users\daniel_administrator\documents\visual studio 2005\projects\newproject\newproject\hello.cpp(7) : error C2065: 'endl' : undeclared identifier

Share this post


Link to post
Share on other sites
Advertisement
at the beginning of the file (below including headers) write:
using namespace std;

or use:
std::cout, std::endl

(and dont forget to: #include <iostream>)

Share this post


Link to post
Share on other sites
When you have problems in future, post the source code too. It makes it a lot easier for us to help you.

Share this post


Link to post
Share on other sites
Danoo,

You're using identifiers which are part of the Standard Namespace. To use them, you must either a. Qualify them with the name of the namespace they are in, or b. use a using statement to bring the contents of the a namespace within the current context.

Option A

void main()
{
std::cout << "Hello World" << std::endl;
}


Option B

void main()
{
using namespace std;
cout << "Hello World" << endl;
}



Note that a "using" statement abides by scoping rules. So to bring the contents of the namespace only within the current function/method, put the using statement within the funtion/method.

To have it apply to the current compilable unit (the entire .cpp file), put the using statement at the top, such as:

Option C

using namespace std;

void main()
{
cout << "Hello World" << endl;
}



Cheers!

Share this post


Link to post
Share on other sites
Hi guys, Thanks for the fast replys.
Although im still unsure. The guide i was reading said to do this:

Quote:

// HelloWorld
#include <ostream.h>
#include "stdafx.h"

int main()
{
cout << "Hello World!" << endl;
return 0;
}


So to get it to work i need to replace(?)
Quote:
int main()
{
cout << "Hello World!" << endl;
return 0;
}

With(?)
Quote:
void main()
{
using namespace std;
cout << "Hello World" << endl;
}

Share this post


Link to post
Share on other sites
Your guide is outdated.

  1. The ostream.h header is deprecated. Instead, you should include <iostream>, which is compliant to the ANSI C++ Standard.

  2. cout and endl are declared within the std namespace, so they should be prefixed with std::. Alternatively, you can add using namespace std; in main's scope, like others have mentioned.

  3. Both void main() and int main() will most likely compile fine. However, according to the Standard, the return type of main() should always be int, for which reasons you can read about here.

  4. Including "stdafx.h" is unnecessary. It's a pre-compiled header added by your IDE.


You might want to consider a more recent guide. Thinking in C++ is an excellent e-book, which is free.

Hope this helps.

[Edited by - Darklighter on July 1, 2007 3:27:02 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!