strings in C++

tailhook123's Avatar, Join Date: May 2007
Go4Expert Member
I've been trying to setup visual studio and have run into a problem getting strings to work.

Code:
// stringtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
	string Mystring("ABC");

	int a = 0;

	return 0;
}
Just trying to get a simple string into the program but it won't recognize the 'string' variable. I've included the #include <string>.. is this a problem with needing to setup an include directory so that VS 2005 knows where to look or something else?
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Just including <string> is not enough. After all, you might define a string type of your own. The string class, as declared by <string> lives in the std namespace. You will need to quality your references by using something like, std::string MyString = "ABC";, or, alternatively, place the statement, "using std::string;" ahead of any usage. You need to be careful with "using" directives. You particularly need to be careful with an all-encompassing statement such as, "using namespace std;".

If you import every name in the std namespace (or even those in just string), you increase your chances of clashes with names that you come up with for your own code.