Skip to main content
gamedev.net
🔒 Locked

Basic Problems with MSVC and C++

Started by BrianMJC May 11, 2003 at 4:02 PM 6 replies 550+ views
Original Post
BrianMJC
BrianMJC
Hi all, I am having great difficulty with MS Visual C++ 6.0 and trying to incorporate some C++ code into my existing C code. I am learning C++ but know C very well. When using C++ strings, one must include string.h, for cin and cout, one must include iostream.h,... and so forth. My compiler, however, does not know what strings, vectors, cin, cout, etc., are and gives me errors. I found that, for some mysterious reason, including istream.h and ostream.h besides iostream.h makes the compiler recognize cin and cout, but it's supposed to recognize it from just including iostream.h. I tried using namespace std, but MSVC doesn't even understand what that means, claiming there is no such namespace as std in an error message. Help is greatly appreciated! Thanks and be well, Brian [edited by - BrianMJC on May 11, 2003 5:04:22 PM]
JuNC
JuNC
quote:

// note that 6.0 needs the .h, while .NET doesn''t.



Without .h works fine for me in 6.0 (and is the correct way).
BrianMJC
BrianMJC
Thanks Anonymous, I just tried it and the compiler now recognizes strings and so forth.

It''s pretty silly though that we have to include the extra typing, which is not needed in standard C++. I wonder if there''s a way to get the compiler to work without it... oh well. At least it works.

BTW, JuNC is right about not needing to specify the .h.

Take care.
dede
dede
Actually, it looks as if the AP likes to type.


#include <iostream>
#include <string>//Can''t remember .h or just string...

using namespace std;

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

string foo = "hello World";

cout << foo << endl;
}


the std:: is needed if you decided to forgo the using namespace std;. If you are using multiple namespaces, then it''s a good idea. If you''re sticking with one nampespace, then its ok.
BrianMJC
BrianMJC
dede -- Thanks, I actually was about to post exactly that, I just figured it out!

I tried putting ''using namespace std;'' in my custom header file originally - this caused MSVC to have a fit and not understand strings and so forth. But it seems one must write ''using namespace std;'' at the top of every .cpp file.

Peace.
Qw3r7yU10p!
Qw3r7yU10p!
hi brian

The C++ standard specifies that the headers for the standard library don''t have .h on them. The reason you see them around is for pre-standard compilers to remain backwards compatible.

In your own code it is a good idea to try to write standard code.

C++ has namespaces which allow you to modularise your code to some extent. The standard library uses them. If you use the pre-standard headers you will be using the non-namespace versions of the library.

If you use the standard library all the classes, functions, variables are declared within namespace std.

''using namespace'' declarations make the names visible but also undermine the whole point of putting them in the namespace in the first place. Never put it in a header and if you can just put what you really need.

eg
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;

The std::iostream library is actually different from the pre-standard one as it makes use of iterators for streams.

Here is a hopefully useful article for you from gotw
Migrating to Namespaces
Difficulty: 4 / 10

Standard C++ includes support for namespaces and name visibility control with using declarations and directives. What''s the best way to use these powerful facilities, and what''s the most effective way to initially migrate your existing C++ code to a namespace-aware compiler and library?
BrianMJC
BrianMJC
Beautiful, petewood -- works great, thanks for the info, very much appreciate it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.