Call of overloaded function(int, int) is ambiguous.

I don't understand why I am getting this error. It was working fine until I overloaded it, by adding a new function with a third parameter.

now the function with 3 parameters works, but the one with only 2 gives me this error.

How can it be ambiguous when its got different amount of parameters???
Well, you should post the two function declarations.
The compiler should also give you more detailed information about why it is ambiguous.
Why is this:
1
2
3
4
5
6
7
8
    inline matrix( unsigned rows = 0, unsigned cols = 0):
      myRows(rows),
      myCols(cols)
      //data  ( (rows && cols) ? (new value_type[ nrows * ncols ]) : NULL )
      {myRows = rows; myCols = cols;  }

    inline matrix(unsigned rows = 0, unsigned cols = 0, int fillValue)
//etc etc 

won't compile but this will:

1
2
inline matrix( unsigned rows, unsigned cols, int fillValue)
//etc etc  
Once you have a default value for a parameter... all parameters that follow it must also have default values.

Think about it... how would you call this ctor and use those default values: inline matrix(unsigned rows = 0, unsigned cols = 0, int fillValue)

1
2
3
4
5
6
7
matrix(5);  // the '5' is the first param, so compiler thinks it's for your rows
  // error because there's no value for 'fillValue'

matrix(5,3);  // again... this just specifies rows/cols.  No value for fillValue
  // still an error

matrix(5,3,2);  // this will work, but you're not using any of the default values, so they're useless 
And you're going to end up with ambiguous calls no matter what if you make fillValue have a default value because
both

matrix m( 5 );
matrix m2( 5, 6 );

won't know which of the two constructors to call.

Best solution:

1
2
3
4
matrix() : myRows(), myCols() {}

matrix( unsigned rows, unsigned cols, int fillValue = 0 ) :
    myRows( rows ), myCols( cols ) { /* etc */ }

Topic archived. No new replies allowed.