Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

my constructor gets ignored somehow. Here is my code:

My class:

class field
{
private:
    char PlayField[5][5];

public:
    char o = 'o';
    field()
    {
        char PlayField[5][5] = { { o, o, o, o, o }, { o, o, o, o, o }, { o, o, o, o, o }, { o, o, o, o, o }, { o, o, o, o, o } };
    }


    void setTile(int x_val, int y_val)
    {
        PlayField[x_val][y_val] = 'x';
    }
    char getTile(int x_val, int y_val)
    {
        return PlayField[x_val][y_val]; 
    }



    /*field::~field();*/
};

The constructor field() should initalize my 4 wins field with 'o's and if I want to add a Tile it will x mark where the tile is. But if I do

int main()
{
    char x;

    field FourWins;
    //FourWins.setTile(3, 2);
    x = FourWins.getTile(3, 2);
    std::cout <<  x << std::endl;

    return 0;
}

The constructor will get ignored and I get a weired sign which is most likely just currently at where I'm looking. The position finding works, becouse if I first set and x to (3,2) it will print me the x.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

Ideone example here

The initialization syntax for char[][] you used is allowed, but only at construction - not assignment (and your example constructed a new variable rather that assigned to the member variable). At least with C++14 you can do it like this:

class field
{
private:
    char o = 'o';
    char PlayField[5][5];
public:
    field() : PlayField{{ o, o, o, o, o }, { o, o, o, o, o }, 
                        { o, o, o, o, o }, { o, o, o, o, o }, 
                        { o, o, o, o, o }}
    {}
};

Consider using std::vector of std::vector, or std::array of std::array instead. The only down side of using std::array compared to char[5][5] is that the sizes (5x5 in your case) must be known at compile time (just as in your example)

#include <iostream>
#include <array>
#include <vector>

using namespace std;

int main() {
    char o='A';

    // C++11 or later:
    std::array<std::array<char,5>, 5> PlayField2{{{o,o,o,o,o},{o,o,o,o,o},
                                  {o,o,o,o,o},{o,o,o,o,o},{o,o,o,o,o}}};

    // or: (C++11 or later)
    std::array<std::array<char,5>, 5> PlayField;
    for(auto& row : PlayField){
        for(auto& place : row){
            place=o;
        }
    }
    cout << PlayField[2][3] << std::endl;

    // or: (C++98 or later)

    std::vector<std::vector<char>> PlayFieldVec(5,std::vector<char>(5,o));
    cout << PlayFieldVec[2][3] << std::endl;

}

The syntax you used for array initialization is fine, just make sure you don't assign to a new variable if you mean to initiate an existing one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...