Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 默认参数:无效使用';这';在非静态成员函数之外_C++_Arguments_Default - Fatal编程技术网

C++ 默认参数:无效使用';这';在非静态成员函数之外

C++ 默认参数:无效使用';这';在非静态成员函数之外,c++,arguments,default,C++,Arguments,Default,我尝试在函数中使用默认参数,但编译器说有一个错误: invalid use of 'this' outside of a non-static member function 我怎样才能解决这个问题 编辑:@RSahu,这是两个重载函数,你能解释一下我如何处理这个问题吗,因为我显然不知道如何修复它 Game.hpp: class Game { private : int** board; vector<pair <int, int> > listPieces

我尝试在函数中使用默认参数,但编译器说有一个错误:

invalid use of 'this' outside of a non-static member function
我怎样才能解决这个问题

编辑:@RSahu,这是两个重载函数,你能解释一下我如何处理这个问题吗,因为我显然不知道如何修复它

Game.hpp:

class Game {
 private :
  int** board;

  vector<pair <int, int> > listPiecesPosition();
  vector<pair <int, int> > listPiecesPosition(int** board);

  // What doesn't work
  vector<pair <int, int> > listPiecesPosition(int** board = this->board); 
类游戏{
私人:
int**板;
向量listPiecesPosition();
矢量列表块位置(int**板);
//什么不起作用
向量listPiecesPosition(int**board=此->板);
Game.cpp:

//Here I need to write more or less two times the same function, how can I do it only once ?

   vector<pair <int, int> > Game::listPiecesPosition() {
vector<pair <int, int> > listPiecesPosition;
for (int i=0; i < getSize(); i++)
  for (int j=0; j < getSize(); j++)
    if (getBoard()[i][j] == nextPlayer.getColor()) // Here I don't use the parameter
      listPiecesPosition.push_back(make_pair(i,j));
return listPiecesPosition;
  }

  vector<pair <int, int> > Game::listPiecesPosition(int** board) {
    vector<pair <int, int> > listPiecesPosition;
    for (int i=0; i < getSize(); i++)
      for (int j=0; j < getSize(); j++)
        if (board[i][j] == nextPlayer.getColor()) // Here I use the parameter
          listPiecesPosition.push_back(make_pair(i,j));
    return listPiecesPosition;
  }
//在这里,我需要编写差不多两次相同的函数,我怎么能只写一次呢?
向量游戏::listPiecesPosition(){
向量表分段定位;
对于(int i=0;i

感谢您的帮助!

只能在非静态成员函数体内使用。因此,您将
此->板
用作输入的默认值是不正确的

我建议创建一个重载来绕过这个问题

class Game {
 private :
  int** board;

  vector<pair <int, int> > listPiecesPosition(int** board);
  vector<pair <int, int> > listPiecesPosition()
  {
     return listPiecesPosition(this->board);
  }

向量游戏::listPiecesPosition(){ 返回listPiecesPosition(此->板); }

通过这样做,可以避免实现函数主逻辑的代码重复。

默认参数必须是编译时可解析的。

此的值只有在运行时才知道。@Danvdb在return语句return listPiecesPosition;?@Cameron:;这只是一个范围问题。@Lightness:rected.谢谢:-)问题是我的函数listPiecesPosition很长,我不想复制两次相同的代码。可以这样做吗:类游戏{private:int**board;向量listPiecesPosition(int**boardArgument=board);向量listPiecesPosition(int**boardArgument){返回listPiecesPosition(boardArgument);}或者我没有任何其他选择来重载我的函数?@Danvdb,你不需要。第二个函数的实现正是我在回答中发布的。@Danvdb,这就是为什么有两个重载函数。如果你用另一块板调用
listPiecesPosition
,第一个重载将被调用。如果你用no参数,第二个重载将被调用。如果这没有意义,现在是时候刷新C++和重载函数的知识。@ DANVDB,不。我为第二个重载发布的代码是实现它所需的全部。@ DANVDB:也许你没有发现一个重载调用另一个重载。
vector<pair <int, int> > Game::listPiecesPosition() {
   vector<pair <int, int> > listPiecesPosition;
   for (int i=0; i < getSize(); i++)
      for (int j=0; j < getSize(); j++)
         if (getBoard()[i][j] == nextPlayer.getColor()) // Here I don't use the parameter
            listPiecesPosition.push_back(make_pair(i,j));
   return listPiecesPosition;
}
vector<pair <int, int> > Game::listPiecesPosition() {
   return listPiecesPosition(this->board);
}