2

I am trying to create a 2D array of objects in a class and then initializing each cell using a constructor from another class, but i'm getting an error and don't know how to fix it. Here are the classes:

Tile:

class Tile
{
public:
    enum Type
    {
        Sea, Ship, Hit, Miss    
    };
    Tile() {}
    Tile (int X, int Y, Type c)
    {
        this->X = X;
        this->Y = Y;
        this->cell = cell;
    }
    void setType(Type c)
    {
        this->cell = c;
    }
    Type getType()
    {
        return cell;
    }
    void draw(bool hidden)
    {
        if (hidden == false)
            switch (this->getType())
            {
                case Sea:
                    cout<<" ~ ";
                    break;

                case Ship:
                    cout<<" s ";
                    break;

                case Hit:
                    cout<<" X ";
                    break;

                case Miss:
                    cout<<" o ";
                    break;

            }
        else
            switch (this->getType())
            {
                case Hit:
                    cout<<" X ";
                    break;

                case Miss:
                    cout<<" o ";
                    break;

                default:
                    cout<<" ~ ";
                    break;
            }
    }
private:
    Type cell;
    int X,Y;
};

Class Board:

class Board
{
private:
    Tile B[row][col];
    int R, C;
public: 
    Board (Tile B[][col])
    {
        for (R = 0; R < row; R++)
            for (C = 0; C < col; C++)
                B[R][C] = new Tile(R, C, Tile::Sea);
    }
};

The error i am getting is inside the constructor of the Board class. I believe it could be done using a vector but i am forced to use an array.

6
  • Tile B[row][col] is not possible unless row and col are known at compile time. Otherwise I'd suggest std::vector<std::vector<Tile>>. If you are "forced to use an array" then as I said, your dimensions must be known at compile time. Commented May 24, 2018 at 11:18
  • Sorry i forgot to mention that row and col are constant declared in the beginning of the program :) Commented May 24, 2018 at 11:20
  • Also don't need new here, just B[R][C] = Tile(R, C, Tile::Sea); Commented May 24, 2018 at 11:21
  • Well, what is the error? Commented May 24, 2018 at 11:22
  • 1
    Because you don't have an array of Tile* you have an array of Tile. You only need new when you want to dynamically allocate an object and get it's pointer. Commented May 24, 2018 at 11:25

2 Answers 2

2

The error here is that new Tile(R, C, Tile::Sea) returns a pointer and NOT an instance itself:

//B[R][C] = new Tile(R, C, Tile::Sea);

Tile * tile = new Tile(R, C, Tile::Sea);
B[R][C] = *tile;

Surely you can simple do it like @CoryKramer suggests as well:

B[R][C] = Tile(R, C, Tile::Sea);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you for your time :)
0

Not really an answer (which was probably given above) but a few observations comments form is too narrow to contain.

I. Tile' constructor should really read

Tile(Int x, int y, Tile c): X(x), Y(y), cell(c) {}

II. And draw only needs one switch:

void draw(bool hidden) {
    switch(type) {
        case Hit:
            cout<<" X ";
            break;
        case Miss:
            cout<<" o ";
            break;
        case Ship:
             if(!hidden) {
                 cout<<" s ";
                 break;
             }
        case Sea:
            cout<<" ~ ";
            break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.