I am using visual C++ 2005(native C++)
I got the error
error C2065: 'CString' : undeclared identifier
for the following code,
#include <cstring>
using namespace std;
int main(){
CString ab;
ab="a";
} what is the problem?
I am using visual C++ 2005(native C++)
I got the error
error C2065: 'CString' : undeclared identifier
for the following code,
#include <cstring>
using namespace std;
int main(){
CString ab;
ab="a";
} what is the problem?
CString is not part of the C++ standard library. In a plain Win32 console project the compiler only sees standard headers, so #include <cstring> (the C header for memcpy, strcmp, etc.) will not declare CString. You have two clean options:
std::string (or std::wstring for Unicode).#include <string>
#include <iostream>
int main() {
std::string ab = "a";
std::cout << ab << '\n';
} CString without pulling in full MFC, include ATL’s string header. This is header-only for CStringT/CString and works in console apps.#include <atlstr.h> // CString / CStringT
#include <tchar.h>
#include <cstdio>
int main() {
CString s = _T("a");
_tprintf(_T("%s\n"), s.GetString()); // prints correctly in ANSI or Unicode builds
} Notes:
<afx.h> or <afxwin.h>, but that is heavy just to get CString.CString converts to LPCWSTR. std::cout does not accept wide strings, so cout << s can print an address. Use _tprintf, wprintf/wcout, or cast appropriately for the active character set ((LPCTSTR)s with std::wcout in Unicode).Jump to Post— Ancient Dragon 5,243native c++ does not support CString -- that is a MFC c++ class. The cstring header file you included is the same as C's string.h, which is not at all like CString or c++ string.
>>what is the problem?
include <string> header file and use std::string object.[edit]If …
native c++ does not support CString -- that is a MFC c++ class. The cstring header file you included is the same as C's string.h, which is not at all like CString or c++ string.
>>what is the problem?
include <string> header file and use std::string object.
[edit]If you are using the Pro edition of that compiler I think you can create a console project that suports MFC non-visual classes such as CString. I know VC++ 6.0 does it, but not sure about VC++ 2005 Pro or newer compilers[/edit]
Since you are developing a Win32 Console Application, you don't get the boiler plate code that gets added for different project types.
Please include <atlstr.h> in your code after the pre-compiled headers.
Hope this resolves the issue.
Cheers,
Rajshree Dugar
Since you are developing a Win32 Console Application, you don't get the boiler plate code that gets added for different project types.
Please include <atlstr.h> in your code after the pre-compiled headers.
Hope this resolves the issue.
Caution:-
If you directly access the CString variable in a Win32 console application, you will get the address and not the content. In order to access the content of the variable you need to convert it to a native C++ type (for eg: std::string). My suggestion is to use std::string instead it is absolutely necessary to use CString. It is very much possible to use CString but not recommended due to conversion overhead. Pick your choice.
Cheers,
Rajshree Dugar
Goto Project -> Settings -> Use MFC in a Shared DLL
Then,
Please try to include #include <afx.h>
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.