strings in C++

Discussion in 'C++' started by tailhook123, Aug 2, 2007.

  1. tailhook123

    tailhook123 New Member

    May 23, 2007
    Likes Received:
    Trophy Points:
    I've been trying to setup visual studio and have run into a problem getting strings to work.

    // 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?
  2. DaWei

    DaWei New Member

    Dec 6, 2006
    Likes Received:
    Trophy Points:
    Semi-retired EE
    Texan now in Central NY
    Home Page:
    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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice