小编Van*_*our的帖子

"读访问违规:这是nullptr"我以为我正确分配了它?

我有一个玩家类,其中包含玩家的名字,正确的答案以及玩家得到的错误答案.当我尝试访问getRight(),getWrong(),addToRight()或addToWrong()函数时,我在这些函数内部的语句中收到一条错误,上面写着"读取访问冲突:这是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
Run Code Online (Sandbox Code Playgroud)

这是Player.cpp文件:

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ pointers class function nullreferenceexception

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

class ×1

function ×1

nullreferenceexception ×1

pointers ×1