Help, very simple C++ code wont work on MSVC.

Started by
11 comments, last by Ice-T 20 years, 8 months ago
#include <iostream>int main(){  char r = 'r';  std::cout << r;}    


If the preceeding code does not work there's something wrong with your libraries and/or install.

[EDIT] What version of MSVC are you using?


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on August 10, 2003 1:04:51 PM]
[size=2]
Advertisement
Lektrix is right to tell you not to use *.h for your include file names. The *.h is a little thing left over from C and pre-ANSI/ISO, non-standard C++. Namespaces should be learned at an early stage. The committee that standardized C++ declared it good programming style to use namespaces to prevent conflict from different header files and such with the same name. Which is very much the same as the way overloaded functions are differentiated.

Out of the examples:

#include <iostream>
using namespace std;
int main()
{
char r = ''r'';
cout << r;
return(0);
}

... and ...

#include <iostream>
int main()
{
char r = ''r'';
std::cout << r;
return(0);
}

Know both of them (why they work and such), but use the first as extensively as possible. This will keep you code simple and clean. Otherwise (not using namespaces), you could very well end up using std::______ for every statement from iostream or any other include file you decide to use. It gets fairly tiresome (^_^)

I do apologize to anyone who was offended by my usage of *.h. Old habits die hard. (^_^)
~Belgarion
Or you could just pull the needed elements into the global namespace, instead of eliminating its purpose by "using namespace std", which effectively aliases std with the global namespace. Like so:

using std::cout;
using std::endl;

Hey, not tiring, and you can prevent global namespace pollution. Sounds like a winner.

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement