C++ “读取访问冲突:这是 nullptr” 以为我正确分配了它......?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36974585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Read Access Violation: This was nullptr" Thought I assigned it correctly...?
提问by Vanidad Velour
I have a Player Class that holds the name of the player, right answers,and wrong answers the player's gotten. When I try to access getRight(), getWrong(), addToRight(), or addToWrong() functions I get an error that says, "Read access violation: this was nullptr" at the statements inside of those functions. I must not be setting my pointer correctly. What changes should I make? Thanks!
我有一个玩家类,其中包含玩家的姓名、正确答案和玩家得到的错误答案。当我尝试访问 getRight()、getWrong()、addToRight() 或 addToWrong() 函数时,我在这些函数内部的语句中收到一条错误消息,指出“读取访问冲突:这是 nullptr”。我一定没有正确设置我的指针。我应该做哪些改变?谢谢!
Here's the Player.h file
这是 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
Here is the Player.cpp file:
这是 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;
}
And here's my main:
这是我的主要内容:
#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);
}
Does a class have to be set up differently when dealing with pointers to avoid these access violations? Thanks!
在处理指针时是否必须以不同的方式设置类以避免这些访问冲突?谢谢!
回答by xaxxon
void test(Player *player) {
...
player = new Player(...);
}
That only changes the local copy of player. To change the pointer outside the function you need to take a reference to the pointer (or a double pointer). Use:
那只会改变玩家的本地副本。要在函数外更改指针,您需要引用指针(或双指针)。用:
void test(Player *& player) {...}
instead.
反而。