问题
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?
回答1:
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.
回答2:
The error you made is, that you define PlayField twice:
class field
{
private:
char PlayField[5][5];
^^^^^^^^ class attribute
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 } };
^^^^ this 'char' made a second PlayField local! a common mistake
}
so, it initialize only the local array, not the class attribute one.
Unfortunately -as far as I know- there is no way to explicit init an array beside using two for loops:
for(int x=0;x<5;x++)
for(int y=0;y<5;y++)
PlayField[x][y]=o;
At the end, your constructor wasn't ignored, it has not the effect you wanted :-)
回答3:
You can't assign value to an array like this. You are defining a local variable in your constructor. You can do like this in your constructor.
for(int i = 0; i < 5; i++){
for (int j = 0; j < 5; ++j) {
PlayField[i][j] = o;
}
}
回答4:
The constructor is being invoked, but is not initialising the CharField member. Instead it is creating a local array named CharField (which, although having the same name, is distinct from the struct member named CharField), initialising it, and that array no longer exists when the constructor completes.
The constructor implementation
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 } };
}
has exactly the same effect (no change of the struct member named CharField) as
field()
{
char unused_data[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 } };
}
The only thing that has changed is the name of the temporary array created in the body of the constructor.
To actually initialise the PlayField member, change this contructor to use an initialiser list
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 } }
{ // body of constructor is here
};
or (before C++11) where this form isn't supported.
field()
{
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
CharField[5][5] = o;
};
来源:https://stackoverflow.com/questions/29869357/constructor-gets-ignored