0

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.

anderssinho
  • 298
  • 2
  • 7
  • 21
  • 2
    The compiler is trying to say that it doesn't know what `ProtocolColumn` is at the place where the error occurs. You forgot to declare it or didn't include the header or have circular dependencies. – nwp Feb 06 '18 at 14:03
  • @nwp Hmm okay.I have included the protocolColumn.h in the yatzee.h file. I might then have some circular dependencies but how can I check that? – anderssinho Feb 06 '18 at 14:06
  • 2
    @anderssinho: No. *Don't* include `protocolColumn.h` in the `yatzee.h` file (other clients of the yatzee.h file don't need it). Include it in `yatzee.cpp` instead. – Martin Bonner supports Monica Feb 06 '18 at 14:07
  • 1
    As a side comment, it would be much better to write your default constructor as: `Yatzee::Yatzee() : Yatzee(1) {}` and delegate to the constructor that takes a number of arguments. – Martin Bonner supports Monica Feb 06 '18 at 14:09
  • @MartinBonner okay, I'll try that. Why should you include in the cpp and not in the h-file? – anderssinho Feb 06 '18 at 14:09
  • 1
    [This](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) is a bit old but seems to still be valid and solve your problem. – nwp Feb 06 '18 at 14:09
  • @nwp Thanks this helped me correct my code! – anderssinho Feb 06 '18 at 14:15
  • 1
    I don't see a circular dependency. I just tested here (VS 2013) and it compiled fine. (Commenting out `#include "Dice.h"` as I don't have that file.) – 001 Feb 06 '18 at 14:18
  • @JohnnyMopp that's weird, I have VS2017 and it doesn't compile for me if I don't rewrite the code like in the link nwp gave me above – anderssinho Feb 06 '18 at 14:20
  • Does `Dice.h` include either `Yatzee.h` or `ProtocolColumn.h`? – drescherjm Feb 06 '18 at 14:23
  • @drescherjm you can remove the dice.h from this example because it doesn't do anything for my problem – anderssinho Feb 06 '18 at 14:25
  • 1
    @anderssinho It reduces coupling (in that users of the `Yatzee.h` header don't drag in the definitions in `ProtocolColumns.h`. It also may eliminate the circular dependencies (the definition of `Yatzee` only requires the *declaration* of `ProtocolColumn`. If the definition of `ProtocolColumn` requires the definition of `Yatzee`, that's fine.) – Martin Bonner supports Monica Feb 06 '18 at 14:27
  • One thing I forgot to say, is that you *will* need `class ProtocolColumn;` at the top of `yatzee.h` if you don't include the header there. – Martin Bonner supports Monica Feb 06 '18 at 14:27
  • I was trying to figure out where the actual problem was since it is not in the code that is shown. A second problem if you did not have circular includes would be reusing the same header guards for different headers. – drescherjm Feb 06 '18 at 14:29
  • @drescherjm okay. But it seems like the problem was in the code above since it got resolved when I used the link nwp gave me. Not sure if there is any difference between VS2013 and VS2017 regarding this type of problems – anderssinho Feb 06 '18 at 14:36

0 Answers0