KBD

Keith Devens .com

Sunday, March 14, 2010 Flag waving
Omit needless words. – William Strunk, Jr. (The Elements of Style)
← CSS3 SelectorsOSX packaging groups join forces →

Daily link icon Saturday, June 21, 2003

Why does Visual Studio suck ASS?

Ok, I'm trying to run some simple code that's essentially right out of the book (The C++ Programming Language (Special Edition), Stroustrup, p483), and Visual Studio can't compile it right:

#include <iostream>
#include <map>

using namespace std;

int main(){
    map<string, int> m;
    m["roar"] = 7;
    cout << "This is a test";
}

And it's giving me errors inside a file called "functional" where some of this stuff is defined, I guess.

And, of course I'd usually think I'm wrong... I'm certainly no C++ guru, and have never used map<>s before. But I downloaded Dev-C++ which uses Mingw as its compiler, which is just a Windows port of GCC 3.2, and it compiles the exact same code with no problem.

What the hell!?

I mean, it can't be just that Microsoft can't implement a basic feature of the Standard Library in the newest version of their C++ compiler, can it?

... and yes, it worked if I did map<char*,int>, but not if I did m[string("roar")] = 7.

Update: Adam proves how little I've actually used C++. The answer, of course, was simple: I had to #include <string>. I thought the string class was built in. The code I copied above was just a small test case. The larger code had strings declared in functions and had functions declared with a return type of string, and the compiler didn't complain. Plus, GCC worked, so I never imagined that would be the problem. What's even more disappointing now, besides the fact that Microsoft's compiler didn't complain when I declared strings without #include-ing <string>, is that GCC's behavior is incorrect because it allowed me to use strings without #including <string>.

Oh well, at least life makes more sense now than when I thought Microsoft's compiler was that fundamentally broken. Plus, I now can write the code I want to write. Thanks Adam.

← CSS3 SelectorsOSX packaging groups join forces →

Comments XML gif

Adam Langley (http://www.imperialviolet.org) wrote:

Maybe you should #include <string>?

Often helps when using strings Smiley

AGL

∴ Adam Langley | 21-Jun-2003 5:19am est | http://www.imperialviolet.org | #2238

Keith (http://www.keithdevens.com/) wrote:

Yaaay! I figured it was something obvious. For some reason I thought strings were built in. Couple that with the fact that I'd declared the return types of functions as strings, and declared strings within functions, and the compiler didn't complain, and GCC worked, I didn't think for a second that that would be the problem.

Thanks Adam!

Keith | 21-Jun-2003 11:30am est | http://www.keithdevens.com/ | #2240

Jaak wrote:

GCC simply includes <string> as part of <iostream> inclusion and that's the reason it works in GCC.

Some people rely on it, despite it's not standarized.

That's one of many reasons why everyone should be using more standarized languages like C# or Java. There's simply less room to "innovate" for compiler/library designers there.

Unless you don't mind having 1 million #ifdefs in your code...

∴ Jaak | 21-Jun-2003 5:49pm est | #2244

M. Bean wrote:

I was wondering how long it would take you to catch that. That was the first thing I thought of when you complained to me initially. "Strings aren't part of the standard types." Then I said "You just have to get used to how Microsoft does things" and left it at that. Yeah, I'm an asshole, sue me.Smiley sticking out its tongue

∴ M. Bean | 21-Jun-2003 7:21pm est | #2245

Keith (http://www.keithdevens.com/) wrote:

Bitch.

Keith | 21-Jun-2003 7:58pm est | http://www.keithdevens.com/ | #2246

MattyT (http://sublimesoftware.blogspot.com) wrote:

Hang on, the standard is very clear on this issue. I haven't got the 'bible' here in front of me but I know this one from memory...iostream (or any component) may_or_may_not include string (or any other component). But, to be portable, a client must always #include the appropriate header files.

So, if you're using string you must #include it. The fact that gcc works is luck. It's not gcc's fault that you didn't include the right header, and nor should it produce an error or warning (though a warning would be nice it's very dear to determine when to deliver that warning).

Jaak, there is no reason to use any #ifdefs. Just include the files that you need and all will be well. Do NOT rely on the libraries including things for you!

I will concede that it's not easy to remember...but hey, one of C++'s principles is to 'trust the developer'.

∴ MattyT | 23-Feb-2005 12:31am est | http://sublimesoftware.blogspot.com | #7052

Keith (http://keithdevens.com/) wrote:

It's not gcc's fault that you didn't include the right header, and nor should it produce an error or warning (though a warning would be nice it's very dear to determine when to deliver that warning).

Yeah, but we're talking about Visual Studio. It is Visual Studio's fault that it didn't complain when I declared things using the string class without it being defined anywhere.

Keith | 23-Feb-2005 1:16pm est | http://keithdevens.com/ | #7054

Keith Gaughan (http://talideon.com/) wrote:

What sucks here isn't so much VS, or GCC, but C and C++, or more to the point, the preprocessor. There's no way of ensuring that includes are local to the file including it, ala Java's import keyword. Including it elsewhere would act as a check that you were using the right libararies, but would otherwise be ignored.

And on that point, wouldn't it be nice if there was a preprocessor directive that would include a file once and once only?

Does the preprocessor suck or what.

∴ Keith Gaughan | 24-Feb-2005 11:28am est | http://talideon.com/ | #7060

Thomas Minor wrote:

Look at '#pragma once' to get files included only once.

∴ Thomas Minor | 8-Feb-2010 1:19pm est | #11490

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive

Generated in about 0.111s.

(Used 8 db queries)