Visual Studio 2012 Ultimate 无法识别字符串类型
Visual Studio 2012 Ultimate won't recognize type String
我正在尝试制作一个可以让我写信给某人的客户端-服务器程序。当我把它放在服务器端时,一切正常。现在我想在客户端实现我为服务器端所做的相同功能,Visual Studio 说我不能,因为他不认识 'string'。我试过使用和不使用字符串的包含,但无论哪种方式都行不通。
客户端的代码如下所示:
#pragma once
//#include <winSock2.h>
//#include <Windows.h>
#include "ClientNetwork.h"
#include "NetworkData.h"
#include <string>
#include <string.h>
class ClientGame
{
public:
ClientGame();
~ClientGame(void);
ClientNetwork* network;
void sendActionPackets();
char network_data[MAX_PACKET_SIZE];
void update();
void sendMessagePacket(string message);
};
Visual Studio 给我“语法错误:标识符 'string'。
然而,我清楚地以所有可能的方式将其包括在内。
此外,我在同一个项目中有相同的代码,但用于我程序的服务器端。在这里:
#pragma once
#include "ServerNetwork.h"
#include "NetworkData.h"
class ServerGame
{
public:
ServerGame(void);
~ServerGame(void);
void update();
void receiveFromClients();
void sendActionPackets();
void sendMessagePacket(string message);
private:
//IDs for the clients connection for table in ServerNetwork
static unsigned int client_id;
//The ServerNetwork object
ServerNetwork* network;
//data buffer
char network_data[MAX_PACKET_SIZE];
};
即使没有#include 字符串/string.h 也能正常工作。有人知道为什么这样做吗?
**编辑
我应该指定,我也尝试以这种方式 "std::string" 将其限定在 std 中。但是,它不会起作用。
**看起来是 "Solved"
删除 "string.h" 包含但保留简单的 "string" 并添加 "std::" 范围解决了它。我仍然很困惑为什么我的服务器端版本没有问题。我不必包含它,也不必限定它的范围。无论如何,我应该没问题!谢谢! :)
您没有限定命名空间。 string
驻留在 std
命名空间中,因此要使用它,您需要完全限定它(std::string
),使用 using
指令引入名称(using std::string
) 或者您可以导入完整的命名空间 (using namespace std;
)。 You want to avoid the last option if at all possible.
我正在尝试制作一个可以让我写信给某人的客户端-服务器程序。当我把它放在服务器端时,一切正常。现在我想在客户端实现我为服务器端所做的相同功能,Visual Studio 说我不能,因为他不认识 'string'。我试过使用和不使用字符串的包含,但无论哪种方式都行不通。
客户端的代码如下所示:
#pragma once
//#include <winSock2.h>
//#include <Windows.h>
#include "ClientNetwork.h"
#include "NetworkData.h"
#include <string>
#include <string.h>
class ClientGame
{
public:
ClientGame();
~ClientGame(void);
ClientNetwork* network;
void sendActionPackets();
char network_data[MAX_PACKET_SIZE];
void update();
void sendMessagePacket(string message);
};
Visual Studio 给我“语法错误:标识符 'string'。 然而,我清楚地以所有可能的方式将其包括在内。
此外,我在同一个项目中有相同的代码,但用于我程序的服务器端。在这里:
#pragma once
#include "ServerNetwork.h"
#include "NetworkData.h"
class ServerGame
{
public:
ServerGame(void);
~ServerGame(void);
void update();
void receiveFromClients();
void sendActionPackets();
void sendMessagePacket(string message);
private:
//IDs for the clients connection for table in ServerNetwork
static unsigned int client_id;
//The ServerNetwork object
ServerNetwork* network;
//data buffer
char network_data[MAX_PACKET_SIZE];
};
即使没有#include 字符串/string.h 也能正常工作。有人知道为什么这样做吗?
**编辑 我应该指定,我也尝试以这种方式 "std::string" 将其限定在 std 中。但是,它不会起作用。
**看起来是 "Solved" 删除 "string.h" 包含但保留简单的 "string" 并添加 "std::" 范围解决了它。我仍然很困惑为什么我的服务器端版本没有问题。我不必包含它,也不必限定它的范围。无论如何,我应该没问题!谢谢! :)
您没有限定命名空间。 string
驻留在 std
命名空间中,因此要使用它,您需要完全限定它(std::string
),使用 using
指令引入名称(using std::string
) 或者您可以导入完整的命名空间 (using namespace std;
)。 You want to avoid the last option if at all possible.