'->' 的基本操作数具有非指针类型向量<object*>
Base operand of '->' has non-pointer type vector<object*>
std::vector<Piece*> player1, player2;
/* Filling player 1 and 2 vith piece
player1.push_back(new Piece()) */
std::vector<Piece*> *currentPlayer, *opponent;
currentPlayer = &player1;
opponent = &player2
for(int i = 0; i < currentPlayer.size(); ++i)
{
// This is where i get the error
// error: base operand of '->' has non-pointer type 'std::vector<Piece*>'
currentPlayer[i]->memberFunctionOfPiece()
}
如您所见,我正在尝试使用指向指针向量的指针。但是在尝试访问向量时得到非指针类型
为什么我不能访问成员函数?
问题是您试图在指针类型上使用方括号:
currentPlayer[i]->memberFunctionOfPiece();
您可以使用 operator[] 或者更好地使用 at 函数
currentPlayer->at(i)->memberFunctionOfPiece();
或
currentPlayer->operator[](i)->memberFunctionOfPiece();
您还可以在 STL 容器上使用范围循环
for(auto&& player : *currentPlayer)
{
player->memberFunctionOfPiece();
}
std::vector<Piece*> player1, player2;
/* Filling player 1 and 2 vith piece
player1.push_back(new Piece()) */
std::vector<Piece*> *currentPlayer, *opponent;
currentPlayer = &player1;
opponent = &player2
for(int i = 0; i < currentPlayer.size(); ++i)
{
// This is where i get the error
// error: base operand of '->' has non-pointer type 'std::vector<Piece*>'
currentPlayer[i]->memberFunctionOfPiece()
}
如您所见,我正在尝试使用指向指针向量的指针。但是在尝试访问向量时得到非指针类型 为什么我不能访问成员函数?
问题是您试图在指针类型上使用方括号:
currentPlayer[i]->memberFunctionOfPiece();
您可以使用 operator[] 或者更好地使用 at 函数
currentPlayer->at(i)->memberFunctionOfPiece();
或
currentPlayer->operator[](i)->memberFunctionOfPiece();
您还可以在 STL 容器上使用范围循环
for(auto&& player : *currentPlayer)
{
player->memberFunctionOfPiece();
}