Which header file is CString in?

  • Thread starter 31si
  • Start date
  • Tags
    File
In summary, the poster is trying to use CString in VC++ and is getting errors. They have included the file AFX.h but still get errors. They have also included the file CP2121 Assessment.h but still get errors. They have also included the file stdafx.h but still get errors. The next error is show below:better Idea. I have now imported #include "atlstr.h"And I get only one error but I think I can get around it.
  • #1
31si
21
0
Hello

Just a quickie. could anyone tell me which header file i have to include to be able to use CString in VC++? I have looking on the net and I can't find one that works. One suggests to use AFX.h but I get more errors with that included than without it.
 
Technology news on Phys.org
  • #2
CString is defined in afx.h
your errors maybe because you include also windows.h
if you include any MFC headers (afx.h), don't include windows.h
if it doesn't solve your problem, tell what errors you got...
 
  • #3
I've included the file you mentioned and I still get the same error.

The attachment is a screen shot of the errors.

Can anyone make sense of them?
 

Attachments

  • errors.JPG
    errors.JPG
    42 KB · Views: 1,280
  • #4
It would be more helpful if you'd post the actual source you're trying to compile.
 
  • #5
Here is the code:

Code:
void IPaddress(){
	CString strTemp;
	struct hostent *host;
	struct in_addr *ptr; // To retrieve the IP Address 

	DWORD dwScope = RESOURCE_CONTEXT;
	NETRESOURCE *NetResource = NULL;
	HANDLE hEnum;
	WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );
	WSADATA wsaData;
	WSAStartup(MAKEWORD(1,1),&wsaData);
	if ( hEnum )
	{
		DWORD Count = 0xFFFFFFFF;
		DWORD BufferSize = 2048;
		LPVOID Buffer = new char[2048];
		WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
		NetResource = (NETRESOURCE*)Buffer;

		char szHostName[200];
		unsigned int i;

		for ( i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ )
		{
			if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY )
			{
				if ( NetResource->lpRemoteName )
				{
					CString strFullName = NetResource->lpRemoteName;
					if ( 0 == strFullName.Left(2).Compare("\\\\") )   
						strFullName = strFullName.Right(strFullName.GetLength()-2);
						gethostname( szHostName, strlen( szHostName ) );
						host = gethostbyname(strFullName);
						if(host == NULL) 
							continue; 
						ptr = (struct in_addr *) 
						host->h_addr_list[0];                    

						// Eg. 211.40.35.76 split up like this.             
						int a = ptr->S_un.S_un_b.s_b1;  // 211           
						int b = ptr->S_un.S_un_b.s_b2;  // 40
						int c = ptr->S_un.S_un_b.s_b3;  // 35
						int d = ptr->S_un.S_un_b.s_b4;  // 76

						strTemp.Format("%s -->  %d.%d.%d.%d",strFullName,a,b,c,d);
						AfxMessageBox(strTemp);
				}
			}
		}

    delete Buffer;
    WNetCloseEnum( hEnum ); 
}

WSACleanup();
}
 
  • #6
31si could you post the FULL set of #include lines from the top? Sometimes windows headers can do funny things if you don't #include them in the correct order.
 
  • #7
The headers are as below:

#include "stdafx.h"
#include "CP2121 Assessment.h"
#include <stdio.h>
#include <String.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <D3DX9.h>
#include <d3d9types.h>
#include <winsock2.h>
#include <iostream>
#include <windows.h>
 
  • #8
So, maybe I am getting in over my head here, but...

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/8e21633d-7d06-4e79-9180-a27a264fa60e/ The poster has error messages very similar to yours, and the responses say:

The name stdafx.h sounds as if it would provide support of MFC but it does not; at least if the project was not generated to support MFC [for example if it was generated as a windows forms project] and if a header for MFC has not been added to stdafx.h after generation. The name stdafx.h is a convention that does not necessarily have anything for afx (MFC) in it...

the express edition of VC even does not have MFC.

From poking around it sounds like stdafx is not an actual standard header file, but is a local file that VC will have generated for just your project. Is it possible you need to edit stdafx to add the include for whatever header really includes CString, or reconfigure your project so that it generates stdafx to do this for you?
 
Last edited by a moderator:
  • #9
I can see what you are saying but If I were to include the String.h file, which is where I think CString is, in the .cpp file would it not import be the same as it importing in one of the other header files?
 
  • #10
I'm not exactly sure, I'm sorry, I don't have experience specifically with MFC. But:

- You are correct that importing the CString header file directly in the .cpp is exactly the same as importing the CString header file in stdafx.h.

- I can definitely tell you CString would NOT be in String.h. String.h is a standard C header and not a Microsoft thing.

The advantage of doing it through stdafx.h, as I understand the link there, is that VC++ is supposed to have facilities for automatically generating stdafx.h's, therefore you don't have to know what the header file for CString is... (Unless you're using VC++ express in which case you apparently don't have MFC access at all...)
 
  • #11
OK, next step anyone got any ideas which header file it is in?
 
  • #12
Right, CString is defined in AFX.h. I have imported it and... I get less errors.

The next error is show below:
 

Attachments

  • new error.JPG
    new error.JPG
    19.2 KB · Views: 1,113
  • #13
better Idea. I have now imported #include "atlstr.h"

And I get only one error but I think I can get around it. I will keep you up to date with advances.
 
  • #14
Problem solved. Thank you all for all of your help you have been a tremendous help.

No doubt, I will be back for more assistance very soon.
 

1. What is a CString?

A CString is a data type in the C++ programming language that represents a sequence of characters.

2. Which header file should I include to use CString?

CString is defined in the <string> header file, so you need to include this header in your code to use CString.

3. Is CString a built-in data type in C++?

No, CString is not a built-in data type in C++. It is defined in the <string> header file and is a part of the Standard Template Library (STL).

4. Can I use CString in both C and C++?

CString is primarily used in C++ and is not supported in C. However, you can use the <cstring> header file to access some of the functionality of CString in C.

5. What are some common operations that can be performed on a CString?

CString supports a variety of operations, including concatenation, comparison, substring extraction, and searching for specific characters or substrings. It also has built-in functions for converting to and from other data types, such as integers and floats.

Similar threads

  • Programming and Computer Science
Replies
2
Views
671
  • Programming and Computer Science
Replies
2
Views
374
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
918
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top