"There is no corresponding function to call" C ++ Templates "
Help, I don't understand why I can't run this piece of code for my homework, and xCode doesn't seem to agree with me when it says that I haven't defined a function. see the error chapter below
template <class Comparable>
Comparable maxSubsequenceSum1( const vector<Comparable> & a, int & seqStart, int & seqEnd){
int n = a.size( );
Comparable maxSum = 0;
for( int i = 0; i < n; i++ )
for( int j = i; j < n; j++ )
{
Comparable thisSum = 0;
for( int k = i; k <= j; k++ )
thisSum += a[ k ];
if( thisSum > maxSum )
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
}
return maxSum;
}
int main(){
vector<int> vectorofints;
vectorofints.resize(128);
for (int i=0; i<vectorofints.size(); i++){
vectorofints[i] = (rand() % 2001) - 1000;
}
maxSubsequenceSum1(vectorofints, 0, 127) //**---->the error i get in xcode is "No matching function for call to maxSubsequenceSum1"
return 0;
}
source to share
Change the signature from
Comparable maxSubsequenceSum1( const vector<Comparable> & a,
int & seqStart, int & seqEnd)
to
Comparable maxSubsequenceSum1( const vector<Comparable> & a,
int seqStart, int seqEnd)
The same problem occurs if you do int & i = 0;
. You cannot initialize a non-const reference from an rvalue. 0
and 127
are temporary objects that end at the end of an expression, temporary files cannot be linked to non-const references.
source to share
The compiler is correct. You are calling maxSubsequenceSum1(std::vector<int>&, int, int)
, you have determinedmaxSubsequenceSum1(std::vector<int>&, int &, int &)
There are 2 quick fixes:
1) Override your function to not use a link.
2) Move your constants into variables and pass them that way.
Note: there is another problem with your code. You are calling the maxSubsequenceSum1 function, but you are not telling it which template parameter to use.
I have been corrected and the correction is correct. The note is invalid.
source to share
You have declared a function that expects two integer references, but the one you call takes two integer values by value. It should be like
vector<int> vectorofints;
vectorofints.resize(128);
for (int i=0; i<vectorofints.size(); i++){
vectorofints[i] = (rand() % 2001) - 1000;
}
int k = 0;
int j = 127;
maxSubsequenceSum1(vectorofints, k, j)
return 0;