gpt4 book ai didi

c++ - 变量或字段 'nameOfVariable' 声明无效不知道为什么 :(

转载 作者:行者123 更新时间:2023-11-28 03:52:39 25 4
gpt4 key购买 nike

我在各种网站上进行了谷歌搜索,但我无法弄清楚为什么我的其中一个函数会出现以下错误。

错误:变量或字段“ConnectClient”声明无效

错误:“套接字”未在此范围内声明

这两个错误都是针对我的代码的第 38 行,也就是...

void ConnectClient(Socket _newConnection)

根据我从其他询问这个问题的帖子中了解到的,它通常归结为与头文件不一致(我没有将其用于这个小测试)或者它们在 void 函数中有一个我不知道的返回类型'有。

注意:我已经删除了与部分游戏代码相关的额外代码,以及我正在测试的一堆注释掉的代码。这个程序是winsock使用的小测试

#include <iostream>
#include <winsock2.h>

using namespace std;
const int serverPort = 31337;
const int serverMaxClients = 10;
SOCKET serverSocket;
SOCKET clientSocketArray[serverMaxClients]; //should this have a -1 on it?

bool clientSlotTaken[serverMaxClients];
sockaddr clientSocketAddresses[serverMaxClients];

int FindFreePlayerSpot()
{
for (int slotIndex = 0; slotIndex < serverMaxClients; slotIndex++)
{
if (clientSlotTaken[slotIndex] == false)
{
return slotIndex;
}
}
return -1;
}

void ConnectClient(Socket _newConnection)
{
int slotIndex = FindFreePlayerSpot();
if (slotIndex == -1)
{
closesocket(_newConnection);
cout << "No player slots available, connection aborted"
}
else //Should have a connection
{
clientSocketArray[slotIndex] = _newConnection;
if (clientSocketArray[slotIndex] == INVALID_SOCKET)
{
cout << "Client Connection Error - INVALID SOCKET " << WSAGetLastError();
}
else //Client should have connected
{
cout << "Client Connected on socket slot [" << slotIndex << "] from " << "\n";
clientSlotTaken[slotIndex] = true;
}
}
}

void StartWinSock()
{
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
{
std::cout<<"WSA Initialization failed!\r\n";
WSACleanup();
system("PAUSE");
}

serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(serverSocket==INVALID_SOCKET)
{
std::cout<<"Server Socket creation failed.\r\n";
WSACleanup();
system("PAUSE");
}

SOCKADDR_IN serverInf;
serverInf.sin_family=AF_INET;
serverInf.sin_addr.s_addr=INADDR_ANY;
serverInf.sin_port=htons(serverPort);

if(bind(serverSocket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
{
std::cout<<"Unable to bind socket!\r\n";
WSACleanup();
system("PAUSE");
}

if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR)
{
std::cout<<"Unable to Listen!\r\n";
WSACleanup();
system("PAUSE");
}

u_long iMode=1;
ioctlsocket(serverSocket,FIONBIO,&iMode);
}


int main()
{
playerShip = new PlayerShip;
bool runningServer = true;

cout << "Starting WinSock\n";
StartWinSock();
cout << "Running on port " << serverPort << "\n";


while (true)
{
//Check for a connection
Socket newConnection = accept(serverSocket,NULL,NULL);
if (newConnection != SOCKET_ERROR)
{
cout << "Incomming connection attempt";
ConnectClient(newConnection);
}
Sleep(1);
}


WSACleanup();
return 0;
}

最佳答案

void ConnectClient(Socket _newConnection)
{
int slotIndex = FindFreePlayerSpot();
if (slotIndex == -1)
{
closesocket(_newConnection);
cout << "No player slots available, connection aborted"
}

这里有两个问题。

1)应该是SOCKET,不是Socket。

2) 上面的 cout 语句末尾缺少分号 (;)。

修复上面的这两个错误,main() 中的相关编译器错误和您的代码至少可以编译。至于能不能用……

关于c++ - 变量或字段 'nameOfVariable' 声明无效不知道为什么 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4905053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com