Thread: why can't I use string???

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    why can't I use string???

    hello,
    well I have been struggling with something that makes absolutely no sense to me for the past hour now.

    for some reason, my compiler (MSVC++ 6.0) will not let me use the C++ string class. I have included the string header, but am guessing it is missing alot of declarations or something.

    Firstly, I tried using a C-style null terminated char array for a string. then I found MSVC++ does not have the strings.h. so now I try to use the C++ string class, and get "undeclared identifier" on the line that my identifier exists on . please take a look at this and try to point out something that I am doing wrong (I am guessing I'll have to write my own string class and include it in a new header to fix it ):

    Code:
    #include "stdafx.h"
    #include "resource.h"
    #include <stdio.h>
    #include <windows.h>
    #include <commdlg.h>
    #include <string>
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    	string szFileName1("");
            string szFileName2("");
    that would declare strings with the identifiers of "szFileName1" and "szFileName2"? correct???? I am using the ("") for safety and clarity purposes. so then I try this:

    Code:
    #include "stdafx.h"
    #include "resource.h"
    #include <stdio.h>
    #include <windows.h>
    #include <commdlg.h>
    #include <string>
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    	char szFileName1[] = "";
            char szFileName2[] = "";
            string F1(szFileName1);
            string F2(szFileName2);
    and I still get the undeclared identifier error . I read up on the error and its description says its usually caused by a variable declaration with no type specified. but I don't see where there are ANY missing types (not unless they are in the included string.h like I said), the variables you see in first example should be of type string, and the second example should be null terminated char arrays (c-style strings) which then get defined as strings called F1 and F2. so can someone here please tell me what the heck I am doing wrong ????

    thank you in advance

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    A quick glance through the C++ forum tells me that the string class is in the std namespace. So, you either declare the variables as std::string or put "using namespace std;" underneath your includes.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    dot h aka .h is a c string header not cpp. You have to compile the file as a cpp file for it to work.

    Code:
    char teststr1[] = { "abcdefghijklmnopqrstuvwxyz"  } ;
    works fine for c.

    edit to add

    You can not use cpp in c.
    Last edited by kryptkat; 12-23-2005 at 06:18 PM.

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    must be something else

    thanks kryptkat! that was my problem (didn't know .h made any difference for dev environment included headers ).
    Last edited by Bleech; 12-23-2005 at 06:23 PM.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Firstly, I tried using a C-style null terminated char array for a string. then I found MSVC++ does not have the strings.h. so now I try to use the C++ string class, and get "undeclared identifier" on the line that my identifier
    Try:
    Code:
    #include <cstring>
    But if you're using C++ then it might be best to get used to std::string's
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    then I found MSVC++ does not have the strings.h
    That's because the C header is <string.h>, not <strings.h>.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The standard C header files are available in C++. You just drop the .h and add a leading c. So <stdio.h> becomes <cstdio>.
    A quick glance through the C++ forum tells me that the string class is in the std namespace. So, you either declare the variables as std::string or put "using namespace std;" underneath your includes.
    Or you can use
    Code:
    using std::string;
    which is what I prefer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 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