What Do I Need to Include to Use CString?

K

KevinSimonson

I'm trying to figure out how to use type "CString". I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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
 
F

Felix Palmen

* KevinSimonson said:
I'm trying to figure out how to use type "CString". I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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
 
S

Saeed Amrollahi

I'm trying to figure out how to use type "CString".  I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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
 
B

Balog Pal

KevinSimonson said:
I'm trying to figure out how to use type "CString". I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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.
 
G

Geoff

I'm trying to figure out how to use type "CString". I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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.
 
Ö

Öö Tiib

I'm trying to figure out how to use type "CString".  I'm looking at
"http://msdn.microsoft.com/en-us/library/aa314317(VS.60).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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top