What Do I Need to Include to Use CString?

Discussion in 'C++' started by KevinSimonson, Oct 25, 2010.

  1. I'm trying to figure out how to use type "CString". I'm looking at
    "http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    and it indicates I
    should be able to write the program below using a constructor that
    takes as argument a C
    string literal.

    #include <cstring>
    #include <iostream>
    #include "stdafx.h"
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    CString abc( "abc");
    cout << "Variable \"abc\" has value \"" << abc << "\".";
    return 0;
    }

    But when I try to compile this I get the error message: "1>c:\<path>
    \cstringtest\
    cstringtest\cstringtest.cpp(11): error C2065: 'CString' : undeclared
    identifier". Is
    there a header file that I need to include before the compiler can
    recognize "CString"? I
    had thought that <cstring> was such a header file. Is there another
    one that I should be
    using?

    Kevin S
     
    KevinSimonson, Oct 25, 2010
    #1
    1. Advertising

  2. KevinSimonson

    Felix Palmen Guest

    * KevinSimonson <>:
    > I'm trying to figure out how to use type "CString". I'm looking at
    > "http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    > and it indicates I
    > should be able to write the program below using a constructor that
    > takes as argument a C
    > string literal.


    Is there a specific reason why you want to use this strange MFC class?
    If there isn't, just use std::string. If there is, you should probably
    find a newsgroup dealing with MFC (or, at least, win32 platform
    programming).

    Regards,
    Felix

    --
    Felix Palmen (Zirias) + [PGP] Felix Palmen <>
    web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
    my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
    http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683
     
    Felix Palmen, Oct 25, 2010
    #2
    1. Advertising

  3. On Oct 25, 4:00 pm, KevinSimonson <> wrote:
    > I'm trying to figure out how to use type "CString".  I'm looking at
    > "http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    > and it indicates I
    > should be able to write the program below using a constructor that
    > takes as argument a C
    > string literal.
    >
    > #include <cstring>
    > #include <iostream>
    > #include "stdafx.h"
    > using namespace std;
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    >   CString abc( "abc");
    >   cout << "Variable \"abc\" has value \"" << abc << "\".";
    >   return 0;
    >
    > }
    >
    > But when I try to compile this I get the error message: "1>c:\<path>
    > \cstringtest\
    > cstringtest\cstringtest.cpp(11): error C2065: 'CString' : undeclared
    > identifier".  Is
    > there a header file that I need to include before the compiler can
    > recognize "CString"?  I
    > had thought that <cstring> was such a header file.  Is there another
    > one that I should be
    > using?
    >
    > Kevin S


    Hi Kevin

    1. Because CString is a class from old MFC framework,
    you have to make your program (let say
    Win32 console application project) MFC enabled.
    In Visual Studio .NET, You can do that in project setting pages,
    when you are going to create your project.
    After that, please check the generated "StdAfx.h" file.
    It has some macros which allow you to use CString
    without including some actual header files.
    2. I guess, you forgot to put newline manipulator at the end of:
    cout << "Variable \"abc\" has value \"" << abc << "\"." << endl;
    3. I guess, because there is no overloaded I/O operators for CString
    object, the output is something like this:
    Variable "abc" has value "0039E970"
    I think, somehow, it is the numeric value of "abc".
    4. No, <cstring> is a Standard C++ header file. It officially a
    wrapper
    around C header file <string.h>. please note, the C++ one doesn't
    have .h
    extension.

    Hope that helps
    Regards
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Oct 25, 2010
    #3
  4. KevinSimonson

    Balog Pal Guest

    "KevinSimonson" <>

    > I'm trying to figure out how to use type "CString". I'm looking at
    > "http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    > and it indicates I
    > should be able to write the program below using a constructor that
    > takes as argument a C
    > string literal.
    >
    > #include <cstring>
    > #include <iostream>
    > #include "stdafx.h"
    > using namespace std;
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > CString abc( "abc");
    > cout << "Variable \"abc\" has value \"" << abc << "\".";
    > return 0;
    > }


    Last time I used CString it needed #include <afx.h>. I guess if you generate
    your project with any of "using MFC" options that line will present in
    stdafx.h.

    > Is there a header file that I need to include before the compiler can
    > recognize "CString"?


    Yes. Normally the main page for a class has the info. On yor link pick
    'CString overview' at the bottom, and the #include is near the bottom.

    > I had thought that <cstring> was such a header file. Is there another
    > one that I should be using?


    <cstring> is a standard library header, that is 'mirror' for the C standard
    header <string.h> putting all its names in std::. the blacklisted stuff
    like strcpy() and friends.

    If you want the standard string, you need #include <string> and use
    std::string, but it is a wild monster FUBAR with the only good thing it is
    being "standard". CString is ways superior in all respects, and it was
    such for almost a decade before the standard was born. But that is another
    story.
     
    Balog Pal, Oct 25, 2010
    #4
  5. KevinSimonson

    Geoff Guest

    On Mon, 25 Oct 2010 06:00:55 -0700 (PDT), KevinSimonson
    <> wrote:

    >I'm trying to figure out how to use type "CString". I'm looking at
    >"http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    >and it indicates I
    >should be able to write the program below using a constructor that
    >takes as argument a C
    >string literal.
    >
    >#include <cstring>
    >#include <iostream>
    >#include "stdafx.h"
    >using namespace std;
    >
    >int _tmain(int argc, _TCHAR* argv[])
    >{
    > CString abc( "abc");
    > cout << "Variable \"abc\" has value \"" << abc << "\".";
    > return 0;
    >}
    >
    >But when I try to compile this I get the error message: "1>c:\<path>
    >\cstringtest\
    >cstringtest\cstringtest.cpp(11): error C2065: 'CString' : undeclared
    >identifier". Is
    >there a header file that I need to include before the compiler can
    >recognize "CString"? I
    >had thought that <cstring> was such a header file. Is there another
    >one that I should be
    >using?
    >
    >Kevin S


    As others have pointed out Cstring is an MFC class. You need to turn
    on MFC support in your solution and #include <afx.h>. The latter is
    automatically added in "stdafx.h" if you use the wizard to create your
    app framework. You can do it as a console application, checking the
    MFC box or as an MFC application. You'll also want to look at the
    microsoft.public.vc.mfc group for MFC-specific questions like these.

    <cstring> is the C++ header that wraps the C <string.h> standard
    header. <string> is the header for the std::string class.
     
    Geoff, Oct 25, 2010
    #5
  6. KevinSimonson

    Öö Tiib Guest

    On 25 okt, 16:00, KevinSimonson <> wrote:
    > I'm trying to figure out how to use type "CString".  I'm looking at
    > "http://msdn.microsoft.com/en-us/library/aa314317%28VS.60%29.aspx",
    > and it indicates I
    > should be able to write the program below using a constructor that
    > takes as argument a C
    > string literal.
    >
    > #include <cstring>
    > #include <iostream>
    > #include "stdafx.h"
    > using namespace std;
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    >   CString abc( "abc");
    >   cout << "Variable \"abc\" has value \"" << abc << "\".";
    >   return 0;
    >
    > }
    >
    > But when I try to compile this I get the error message: "1>c:\<path>
    > \cstringtest\
    > cstringtest\cstringtest.cpp(11): error C2065: 'CString' : undeclared
    > identifier".  Is
    > there a header file that I need to include before the compiler can
    > recognize "CString"?  I
    > had thought that <cstring> was such a header file.  Is there another
    > one that I should be
    > using?


    Around 1998 Microsoft MFC had class CString.

    Starting from VC++ (2002 i think) Microsoft has a mess of macroes and
    templates that causes usage of name CString to compile but it is not
    class anymore. Actually there are two separate implementations. One
    comes with <afx.h> other comes with <atlstring.h>. The two are not
    binary compatible with each other.
     
    Öö Tiib, Oct 26, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf Magnus
    Replies:
    2
    Views:
    629
    Karl Heinz Buchegger
    Nov 28, 2003
  2. Elie Nader
    Replies:
    1
    Views:
    666
  3. Tuckers
    Replies:
    18
    Views:
    646
    ThosRTanner
    May 18, 2005
  4. Replies:
    3
    Views:
    436
    Jacek Dziedzic
    Oct 8, 2006
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    1,009
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page