Saturday, November 12, 2011

Microsoft Visual Studio: string Type, Precompiled Header

OK I figured this out, having programmed Visual C++ since 1995 without ever using the standard string type, but just about every other string type there is. The answer is use this in the header:

#include "stdafx.h"
using namespace std;

Now if you get this message:

warning C4627: '#include ': skipped when looking for precompiled header use

you will need to turn off precompiled headers from the menu:
menu bar:
  project properties
         precompiled headers

Here was the original question: 

I just saw this textbook Starting Out with C++: Early Objects (7th Edition) which uses the "standard library string class" which I don't have any recollection of working in Microsoft Visual Studio C++. Strings are just char [] arrays from C, or you can use MFC strings or OLE strings. C# uses a real string type similar to the good old strings they used in BASIC which was the first popular language that did anything intelligent with strings, null terminated character arrays were just a horrible idea, other than they were simple to implement. 

Anyways, this does NOT work in Visual Studio, anybody out there know how to make it work with the textbook since it's so common? The appendix alleges it works fine with Visual Studio.

// string.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include // these define all the crazy microsoft string hacks, NOT the standard library

int _tmain(int argc, _TCHAR* argv[])
string m = "me'      // does not recognize either string or String as defined
String me = "me";
return 0;

No comments: