Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What Do I Need to Include to Use CString?

Reply
Thread Tools

What Do I Need to Include to Use CString?

 
 
KevinSimonson
Guest
Posts: n/a
 
      10-25-2010
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
 
Reply With Quote
 
 
 
 
Felix Palmen
Guest
Posts: n/a
 
      10-25-2010
* 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
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      10-25-2010
On Oct 25, 4:00*pm, KevinSimonson <kvnsm...@hotmail.com> 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
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      10-25-2010
"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.


 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      10-25-2010
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.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      10-26-2010
On 25 okt, 16:00, KevinSimonson <kvnsm...@hotmail.com> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
When to use #include <> and #include " " Tuckers C++ 18 05-18-2005 08:42 AM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Elie Nader C++ 1 11-28-2003 03:12 PM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Rolf Magnus C++ 2 11-28-2003 12:26 PM
#include "bar" negates #include <string> ; how to fix? Danny Anderson C++ 5 08-15-2003 06:38 PM



Advertisments