Thread: string: undeclared indentifier

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    string: undeclared indentifier

    Dear All,
    Although I put preprocessor command
    #include<string>
    my variables are not recognized as of string type and I get error c2065. What is the problem?
    Thank you

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::string
    or
    using namespace std;

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need to use "std::string", "using std::string" or "using namespace std", as modern C++ has "namespaces" , and the string implementation is in the "std" namespace.


    Namespaces avoid conflicts between different names in different portions of the code, and avoid having to put "ModuleX" in front SomeFunction() to make it unique to ModuleX. This is important for large projects that have many different source files, and for example "FindCustomer()" or "OpenInputFile" may be function names that makes sense to many different parts of the code - but you don't want the invoice module calling the "FindCustomer" of the late payment penalty module, for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Thank you for the answer!!
    I have got a question: if I have many source and hearder files in the project, in which I use type string or any other type - Do I have to put line
    using namespace std;
    in each of these header or source files or there is a way to implement std namespace (or any other) once for the whole project?
    Thank you!!!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use in every source file is safest. You can also use it in a header and include it in every source file (but this isn't recommended).
    You must have it on a .cpp file basis.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason you don't want to put it in a header file is that it's then not obvious where your "string" comes from. It may well be that someone implements a "better" improved::string version, that wholly replaces the std::string. Now, if you use an include file to include "using namespace std", then you don't know by looking at a file if it's using std::string or improved::string, do you?

    That's of course just one of many examples.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to have say

    using namespace std;

    or better
    using std::string

    or best of all
    std::string myvar;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string undeclared ?
    By liuguobiao in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2009, 08:57 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM