I'm working on a program which is required to use a pointer to pointer type of variable. I'm getting some error-messages from Visual Studio that I can't interpret and need help to figure out.
I have the following classes:
(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();
}
Every function here is not complete. But my problems lies in the pointer in the Yatzee.h file called ProtocolColumn **ptr. When I try to initiate it in the constructor I get these error codes from Visual Studio:
- C2143 - Syntax error missin ";" before'*'
- C4430 - Missing type specifier - int assumed
- C2238 - Unspecified token(s) preceding ';'
All errors is in the Yatzee.h file and I can understand why I get them. I'm just trying to initiate my **pointer so that I can point to a ProtocolColumn object also.