Sign in to follow this  
soiheardyoulike

Trouble with hello world? Wont compile with visual studio c++ express?

Recommended Posts

Hello again. After searching through my schools directory it turns out there is a class on the basics of programming(that is the aspect i want to get into with gamedev). I emailed the teacher on my intent on doing so , he said after school i can ask him questions if i need help. He says i can borrow the classes book from the library on C++ Programming. Many people said it was daunting and not a beginners choices but then again knowledge is never wasted, and i do have someone who can help. Here is where the trouble starts. I downloaded a compiler called Microsoft visual studio c++ 2008 and it installed successfully. I did the first example called "Hello World". Here is my code. #include <iostream> using namespace std; void main() { cout << "Hello World!" << endl; } The problem is I am getting errors doing so. LNK1104: cannot open file '.\Debug\HelloWorld.obj' and here is another one 1>Project : error PRJ0003 : Error spawning 'C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe'. what should i do? Is this a sign of things to come?

Share this post


Link to post
Share on other sites
The good news is your code is correct -- although exhibiting some minor style issues and one inaccuracy. I'll address that later.

It looks like the IDE is telling you it can't run cl.exe, which is the compiler. Perhaps you lack permissions or write access? Try saving the project (you are creating a 'project' and not just a single source file, right?) somewhere else, like your desktop. Alternatively if you run the 'VS 2008 Command Prompt' from the Start Menu entry for VS and type 'where cl' you can see if cl got correctly installed (e.g., if it gets found by where). If not you may need to reinstall because your installation got whacked somehow.

Here is what I would suggest for your code:

#include <iostream>

int main() {
std::cout << "Hello, world\n";
}

"using namespace std" pollutes the global namespace. It's better in a source file than a header file, but ideally I prefer to avoid it altogether and explicitly qualify the namespaces for things like cout. This is slightly stylistic in nature.

main() shall have a return type of int, according to the standard.

'endl' is equivalent to '\n' plus flushing the stream. It doesn't matter which you use here, I'm just illustrating an alternative; this is mostly stylistic as well.

main() is a special case in that the standard dictates that omitting a return statement from main is the same as return 0. Some people find this uncomfortable, but I prefer the brevity.

Share this post


Link to post
Share on other sites
Thanks for the advice and now it is working(uninstalled, reinstalled, and i searched for a patch just to be sure). Also thanks for the advice on int main() instead of what i am doing(because you a professional and know much more about the popper way it should be done).

Share this post


Link to post
Share on other sites
It might be a compiler specific problem, but try:

#include <iostream>

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

I noticed no return. Your compiler might be having a problem with the namespace std. I notice a void main() instead on int main(), and no return 0;.

Again perhaps its compiler specific for requiring those things, but as far as I know all functions/classes must return something.

Share this post


Link to post
Share on other sites
Quote:
Original post by Refacull
I noticed no return. Your compiler might be having a problem with the namespace std. I notice a void main() instead on int main(), and no return 0;.

Again perhaps its compiler specific for requiring those things, but as far as I know all functions/classes must return something.


Sigh.

main() must return int in C++, per the C++ language standard. It may return void in C, per the C language standard, although it's arguable that this was not really intended.

In C++, there is no requirement to 'return 0;' explicitly at the end of main(). If the end of main() is reached, 0 is returned implicitly. This is not true for any other function; if they reach the end, and the return type is anything other than void, [the return value is unspecified. This is only marginally better than undefined behaviour; if the calling code happens to use the return value, it is using a totally garbage value, un-defining the behaviour of the subsequent code.]* In any case, this is Bad. Of course, if you explicitly return from main(), you must explicitly return some value (whether 0 or something else).

And classes don't return anything, because they aren't called; member functions/operator overloads of the class do (because they are). (Of course, one of the operators that can be overloaded is the () operator (pronounced "call operator", which can make it look as if the instance is actually a function.) The constructor of a class describes code that is run to set up an instance, but you can't really say that it "returns" that instance, because you don't specify a return type for constructors, and because it can be invoked upon an already-allocated instance-size chunk of memory (via 'placement new'); object construction in C++ is actually a two-step process (memory allocation and object initialization) that is typically split up for you behind the scenes.

* The part in square brackets has not actually been fact-checked.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
* The part in square brackets has not actually been fact-checked.


I've checked it out. In debug mod, it returns 0 automatically:

Free Image Hosting at www.ImageShack.us


However, in release mod it returns some rubbish data:


Free Image Hosting at www.ImageShack.us


At any rate, this is clearly an undefined behavior and should be avoided.(who am I to talk... if warnings were radiation I would have been turned into a cool mutant a long time ago...)

Share this post


Link to post
Share on other sites
If you're setting up a project in VS and you are doing a basic console test (such as pretty much any code example in whatever book or tutorial you are following) you should remember to select "new console application" and then on the next window that shows up in the bottom 'tab' on the left check off the "empty project" or you may end up with a bunch of other stuff being added to your new project that may cause it to break in certain situations. (ie: removing the include stdafx.h for the precompiled header.)

I do not know if this is the issue you are having, but it is important to be aware of.

If you are just modifying a single file on its own, chances are you'll need to set up a new project for it first doing the things I detailed above. Good luck! Programming is like learning a new spoken language or like picking up a new instrument. Programming requires technical finesse, creativity, and dedication.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this