所以我试图整齐地设置我的代码做我通常做的事情,但现在我在运行时遇到这个错误
First-chance exception at 0x01313D46 in Server.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
Visual Studio 还在 locals 框中告诉我它无法读取内存
unsigned short port
bool listening
这是发生错误的标头和 Cpp
#pragma once
#include <SFML\Network.hpp>
#include <iostream>
using namespace std;
class NetServer
{
private:
bool listening;
unsigned short port;
public:
NetServer(void);
~NetServer(void);
void InitNetServer();
void SetListening(bool a)
{
a = listening;
}
bool IsListening()
{
return listening;
}
void SetPort(unsigned short a)
{
a = port;
}
int GetPort()
{
return port;
}
};
当它变得太 SetListening(bool a) 时调试会中断,然后如果我把它拿出来,它会在 SetPort 中断
这是我的cp
#include "NetServer.h"
sf::TcpListener listener;
sf::TcpSocket client;
NetServer::NetServer(void)
{
//listener.listen(21025);
//cout << "listening" << endl;
while (true)
{
if (listener.accept(client) == sf::Socket::Done)
{
cout << "New connection received from " << client.getRemoteAddress() << endl;
}
}
}
NetServer::~NetServer(void)
{
SetPort(NULL);
SetListening(false);
}
void NetServer::InitNetServer()
{
cout << "Initialising the NetServer" << endl;
SetListening(true);
SetPort(45000);
listener.listen(GetPort());
cout << "NetServer Initialised : Listening on Port "<< GetPort() << endl;
}
这是我唯一的另一个类,它也在 GameServer.h/cpp 中调用
#include "GameServer.h"
GameServer::GameServer()
{
Run();
}
GameServer::~GameServer(void)
{
DestroyNetServer();
Exit();
}
void GameServer::Run()
{
cout << "Initialising the engine." << endl;
/* Initialise stuff here */
_NetServer->InitNetServer();
/* Finish initialisation */
SetGameRunning(true);
while(GetGameRunning() == true)
{
_NetServer;
}
}
void GameServer::Exit()
{
exit(0);
}
NetServer & GameServer::GetNetServer()
{
if(_NetServer == 0) throw exception ("Null Game Server");
return *_NetServer;
}
void GameServer::CreateNetServer()
{
_NetServer = new NetServer();
}
void GameServer::DestroyNetServer()
{
delete _NetServer;
}
#pragma once
#include "NetServer.h"
#include <iostream>
using namespace std;
class NetServer;
class GameServer
{
public:
GameServer();
~GameServer(void);
void Run();
void Exit();
void SetGameRunning(bool running)
{
gameRunning = running;
}
bool GetGameRunning()
{
return gameRunning;
}
void CreateNetServer();
NetServer & GetNetServer();
void DestroyNetServer();
private:
bool gameRunning;
NetServer * _NetServer;
};