Public Group

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

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

## 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 on other sites
Just so you know, it's int main(), not void main(). void main() is not C++, nor is it C. After googling "PRJ0003", I got this. Hopefully that provides some help. As for the first one, I'm not sure.

##### 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 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 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 on other sites
Quote:
 Original post by RefacullI 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 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:

However, in release mod it returns some rubbish data:

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 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.

• 11
• 19
• 12
• 34
• 9
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999838
×