我正在研究一个程序,它需要使用指向变量指针类型的指针。我从Visual Studio收到一些我无法解释的错误消息,需要帮助才能搞清楚。
我有以下课程:
(Filename: Yatzee.h)
#pragma once
#include <string>
#include <iostream>
#include "ProtocolColumn.h"
using namespace std;
class Yatzee
{
public:
Yatzee();
Yatzee(int nrOfPlayer);
~Yatzee();
void addPlayer(string name);
private:
string name;
int nrOfPlayers;
ProtocolColumn **ptr;
void initiate();
void free();
};
(Filename Yatzee.cpp)
#include "Yatzee.h"
Yatzee::Yatzee()
{
this->nrOfPlayers = 1;
this->ptr = new ProtocolColumn*[1];
this->initiate();
}
Yatzee::Yatzee(int nrOfPlayers)
{
this->nrOfPlayers = nrOfPlayers;
this->ptr = new ProtocolColumn*[this->nrOfPlayers];
this->initiate();
}
void Yatzee::initiate()
{
for (int i = 0; i < this->nrOfPlayers; i++)
{
this->ptr[i] = nullptr;
}
}
void Yatzee::free()
{
for (int i = 0; i < this->nrOfPlayers; i++)
{
delete this->ptr[i];
}
delete[] this->ptr;
}
Yatzee::~Yatzee()
{
this->free();
}
(Filename: ProtocolColumn.h)
#pragma once
#include <string>
#include <sstream>
using namespace std;
class ProtocolColumn
{
public:
ProtocolColumn();
ProtocolColumn(string name);
~ProtocolColumn();
bool addResult(int diceValue, int result);
string getPlayerName()const;
int getSum()const;
bool isFilled()const;
string toString()const;
private:
string name;
int sum;
int *resultDice;
bool *visited;
void initiateProtocol();
};
(Filename: ProtocolColumn.cpp)
#include "ProtocolColumn.h"
ProtocolColumn::ProtocolColumn()
{
this->name = "Joe Doe";
this-> sum = 0;
this->initiateProtocol();
}
ProtocolColumn::ProtocolColumn(string name)
{
this->name = name;
this->sum = 0;
this->initiateProtocol();
}
ProtocolColumn::~ProtocolColumn()
{
}
//Initiate array for the protocol
void ProtocolColumn::initiateProtocol()
{
this->resultDice = new int[6];
this->visited = new bool[6];
for (int i = 0; i < 6; i++)
{
resultDice[i] = 0;
visited[i] = false;
}
}
bool ProtocolColumn::addResult(int diceValue, int result)
{
bool res = false;
if (diceValue < 7 && diceValue > 0 && visited[diceValue-1] == false)
{
this->resultDice[diceValue-1] = result;
this->visited[diceValue - 1] = true;
this->sum += result;
res = true;
}
return res;
}
string ProtocolColumn::getPlayerName() const
{
return this->name;
}
int ProtocolColumn::getSum() const
{
return this->sum;
}
bool ProtocolColumn::isFilled() const
{
bool isAllFilled = false;
for (int i = 0; i < sizeof(visited); i++)
{
if (visited[i]==true)
{
isAllFilled = true;
}
else
{
isAllFilled = false;
}
}
return isAllFilled;
}
string ProtocolColumn::toString() const
{
stringstream ss;
ss << this->getPlayerName() + "\n";
for (int i = 0; i < 6; i++)
{
ss << i+1 << "\t : " << this->resultDice[i] << endl;
}
ss << "--------------------\n";
ss << "SUM = " << this->getSum() << endl;
return ss.str();
}
此处的所有功能都不完整。但我的问题在于Yatzee.h文件中的指针,名为ProtocolColumn ** ptr。当我尝试在构造函数中启动它时,我从Visual Studio中获取这些错误代码:
所有错误都在Yatzee.h文件中,我可以理解为什么会得到它们。 我只是尝试启动我的**指针,以便我也可以指向ProtocolColumn对象。