"Read Access Violation: This was nullptr" 以为我分配正确...?
"Read Access Violation: This was nullptr" Thought I assigned it correctly...?
我有一个玩家 Class,其中包含玩家的姓名、正确答案和玩家获得的错误答案。当我尝试访问 getRight()、getWrong()、addToRight() 或 addToWrong() 函数时,我收到一条错误消息,在这些函数内部的语句中显示 "Read access violation: this was nullptr"。我一定没有正确设置我的指针。我应该做哪些改变?谢谢!
这是 Player.h 文件
#ifndef PLAYER_H
#define PLAYER_H
#pragma once
using namespace std;
class Player;//FWD declaration
class Player
{
public:
Player();
Player(string playerName);
string getName() const
{
return name;
}
//These functions show stats from
//current round
int getRight() const
{
return right;
}
int getWrong() const
{
return wrong;
}
//These functions update
//player info that will be saved
//to player profile
void setName(string userName);
void addToRight();
void addToWrong();
private:
string name;
int right;
int wrong;
};
#endif
这是 Player.cpp 文件:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
name = "";
right = 0;
wrong = 0;
}
Player::Player(string playerName)
{
ifstream inFile;
ofstream outFile;
string name = playerName;
string fileName = playerName + ".txt";
inFile.open(fileName.c_str());
if (inFile.fail())
{
outFile.open(fileName.c_str());
outFile << 0 << endl;
outFile << 0 << endl;
outFile.close();
inFile.close();
setName(playerName);
right = 0;
wrong = 0;
cout << "Welcome new player!"
<< " Your statistics profile has been created." << endl;
}
else
{
inFile >> right;
inFile >> wrong;
inFile.close();
setName(playerName);
cout << "Welcome back!" << endl;
}
}
void Player::setName(string userName)
{
name = userName;
}
void Player::addToRight()
{
right = right + 1;
}
void Player::addToWrong()
{
wrong = wrong + 1;
}
这是我的主要内容:
#include <iostream>
#include <string>
#include "Player.h"
using namespace std;
void test(Player *player);
int main()
{
Player *player = nullptr;
test(player);
cout << "name: " << player->getName() << endl;
cout << "right: " << player->getRight() << endl;
player->addToRight();
cout << "right: " << player->getRight() << endl;
return 0;
}
void test(Player *player)
{
string name;
cout << "name: ";
getline(cin, name);
player = new Player(name);
}
在处理指针以避免这些访问冲突时,class 是否必须进行不同的设置?谢谢!
void test(Player *player) {
...
player = new Player(...);
}
那只会改变播放器的本地副本。要在函数外部更改指针,您需要引用指针(或双指针)。使用:
void test(Player *& player) {...}
相反。
我有一个玩家 Class,其中包含玩家的姓名、正确答案和玩家获得的错误答案。当我尝试访问 getRight()、getWrong()、addToRight() 或 addToWrong() 函数时,我收到一条错误消息,在这些函数内部的语句中显示 "Read access violation: this was nullptr"。我一定没有正确设置我的指针。我应该做哪些改变?谢谢!
这是 Player.h 文件
#ifndef PLAYER_H
#define PLAYER_H
#pragma once
using namespace std;
class Player;//FWD declaration
class Player
{
public:
Player();
Player(string playerName);
string getName() const
{
return name;
}
//These functions show stats from
//current round
int getRight() const
{
return right;
}
int getWrong() const
{
return wrong;
}
//These functions update
//player info that will be saved
//to player profile
void setName(string userName);
void addToRight();
void addToWrong();
private:
string name;
int right;
int wrong;
};
#endif
这是 Player.cpp 文件:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
name = "";
right = 0;
wrong = 0;
}
Player::Player(string playerName)
{
ifstream inFile;
ofstream outFile;
string name = playerName;
string fileName = playerName + ".txt";
inFile.open(fileName.c_str());
if (inFile.fail())
{
outFile.open(fileName.c_str());
outFile << 0 << endl;
outFile << 0 << endl;
outFile.close();
inFile.close();
setName(playerName);
right = 0;
wrong = 0;
cout << "Welcome new player!"
<< " Your statistics profile has been created." << endl;
}
else
{
inFile >> right;
inFile >> wrong;
inFile.close();
setName(playerName);
cout << "Welcome back!" << endl;
}
}
void Player::setName(string userName)
{
name = userName;
}
void Player::addToRight()
{
right = right + 1;
}
void Player::addToWrong()
{
wrong = wrong + 1;
}
这是我的主要内容:
#include <iostream>
#include <string>
#include "Player.h"
using namespace std;
void test(Player *player);
int main()
{
Player *player = nullptr;
test(player);
cout << "name: " << player->getName() << endl;
cout << "right: " << player->getRight() << endl;
player->addToRight();
cout << "right: " << player->getRight() << endl;
return 0;
}
void test(Player *player)
{
string name;
cout << "name: ";
getline(cin, name);
player = new Player(name);
}
在处理指针以避免这些访问冲突时,class 是否必须进行不同的设置?谢谢!
void test(Player *player) {
...
player = new Player(...);
}
那只会改变播放器的本地副本。要在函数外部更改指针,您需要引用指针(或双指针)。使用:
void test(Player *& player) {...}
相反。