私はプレーヤーの名前、正解、プレイヤーが間違った答えを保持するPlayer Classを持っています。 getRight()、getWrong()、addToRight()、またはaddToWrong()関数にアクセスしようとすると、これらの関数内のステートメントで「読み取りアクセス違反:これはnullptrでした」というエラーが表示されます。私はポインタを正しく設定してはいけません。私は何を変えるべきですか?ありがとう!ここで"読み取りアクセス違反:これは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);
}
がするクラスを持っていますこれらのアクセス違反を避けるためにポインタを扱うときは、別の方法で設定しますか?ありがとう!
'using namespace std'と書くのは避けてください。 – Destructor
を除いて、オーバーフローするスタックに投稿する例を除いて、それは問題ありません。また、それを.cppファイルに入れるだけであれば、それほど悪くはありません。実際のコードの.hファイルに入れないでください。 – xaxxon
ポインタの使用理由は何ですか?あなたはすべてを書きました。何らかの理由であなたのプログラムに 'Player * 'を導入しました。どうして? – PaulMcKenzie