繁体   English   中英

错误:重载的调用不明确

[英]error: call of overloaded is ambiguous

bool find_solutions(const string if_need_all, vector< vector<char> > table, vector<int> ships, int row[], int col[]){
  sort(ships.begin(), ships.end(), greater<int>());//sort the ship length in descending order
  static int counter = 0; //counter that tracks if the ship is to be placed vertically or horizontally
  static int s = 0; //index of ship using
  int fill;//ship to fill
  int filling;//keep track how much the ship has been filled 
  if(s == ships.size()) return true;
  for(unsigned int i = 0; i<table.size(); ++i){
    filling = 0;
    fill = ships[s];
    for(unsigned int j = 0; j<table[i].size(); ++j){
      if(counter == 0 && insertable(table,row,col,i,j,counter,fill)){
        while(filling<fill){
          table[i][j+filling] = fill;
          col[j+filling]--;
          filling++;
        }
        row[i] -= fill; s++;
        find_solutions(if_need_all, table, ships, row,col);
      } 
      else{
        counter++;
      }
      if(counter == 1 && insertable(table,row,col,i,j,counter,fill)){
        while(filling<fill){
          table[i+filling][j] = fill;  
          row[i+filling]--;
          filling++;
        }
        col[j] -= fill; s++;
        find_solutions(if_need_all, table, ships, row, col);
      }
      else{
        counter--;
      }
    }
  }
  if(s != ships.size()) return false;
  else return true;
}
main.cpp: In function ‘bool find_solutions(std::__cxx11::string,     std::vector<std::vector<char> >, std::vector<int>, int*, int*)’:
main.cpp:277:67: error: call of overloaded     ‘insertable(std::vector<std::vector<char> >&, int*&, int*&, unsigned int&,    unsigned int&, int&, int&)’ is ambiguous
       if(counter == 0 && insertable(table,row,col,i,j,counter,fill)){
                                                               ^
main.cpp:13:6: note: candidate: bool insertable(const     std::vector<std::vector<char> >&, const int*, const int*, int, int, int, int)
 bool insertable(const vector< vector<char> >& inboard, const int r[], const int c[],
      ^
main.cpp:125:6: note: candidate: bool insertable(std::vector<std::vector<char> >, const int*, const int*, int, int, int, int)
 bool insertable(const vector< vector<char> > inboard,const int r[], const int c[],co
      ^

谁能告诉我我犯了什么错误? 我在网上搜索了一些网站,说它要么是多次创建的变量,要么是STL库中已经存在函数名。 我检查了两个条件,它们不适用于我的问题。 是填充导致问题或其他变量,还是函数?

您已使用以下参数类型重载了insertable()

bool insertable(const std::vector<std::vector<char> >&,
                const int*, const int*, int, int, int, int)

bool insertable(std::vector<std::vector<char> >,
                const int*, const int*, int, int, int, int)

让我们假设T

typedef std::vector<std::vector<char> > T;

这两个方法定义中的第一个参数都是模棱两可的,因为两个方法都可以接受类型TT&作为第一个参数,因此编译器无法确定要调用哪个重载方法。 顺便说一句,您不应创建reference不同的重载方法,而应使用完全不同的类型。 由于类型及其引用始终兼容,因此被视为相同。

对于前。 intint&兼容,因此insertable(int a)insertable(int& b)相同。 如果像下面这样调用此方法,则编译器无法确定要调用的方法。

int x = 20;
insertable(x);

同样,如果在函数调用中使用非const T ,则以下定义​​也相同。

bool insertable(const T&, const int*, const int*, int, int, int, int)

bool insertable(T, const int*, const int*, int, int, int, int)

@MM的评论:

Tconst T&总是模棱两可。 但是,当参数为rvalue时, TT&并不确定。 rvalue不能绑定到T& ,这是一个有效的用例,可以单独重载rvalues而不是lvalues

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM