Jimmy Li
Jimmy Li

Reputation: 53

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
      ^

can anyone tell me what mistake i've made? I searched online that websites says it's either variable created several times or function name already exists in STL library. I checked both conditions and they do not apply to my problem. Is it the fill causing problem or other variable, or it's the function ?

Upvotes: 1

Views: 9255

Answers (1)

jblixr
jblixr

Reputation: 1385

You have overloaded insertable() with the below parameter types.

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)

Let us assume T as

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

The first parameter in both method definition is ambiguous because both methods can accept types T and T& as first parameter, So the compiler can't decide which overloaded method to invoke. BTW you should not create overloaded methods which differs in reference instead you should use an entirely different type. Because a type and its reference are always compatible hence considered as same.

For ex. int and int& are compatible, so insertable(int a) and insertable(int& b) are same. If you call this method like below the compiler can't decide which method to call.

int x = 20;
insertable(x);

In the same way, below definitions are also same if you used non const T in function call.

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

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

@M.M in comments:

T and const T& are always ambiguous. However T and T& are not ambiguous when the argument is an rvalue. The rvalue cannot bind to T&, and it is a valid use case to overload separately for rvalues than lvalues

Upvotes: 2

Related Questions