3

Help I don't understand why i can not run this snippet of code it is for a homework assignment and xCode seems to disagree with me when it says I havent defined the function. see bellow in main for the error

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;
}

3 Answers3

2

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 happens if you would do int & i = 0;. You cannot initialize a non-const reference from an rvalue. 0 and 127 are temporary objects that expire at the end of the expression, temporaries cannot bind to non-const references.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • Wow thank you my professor actually wrote that part so i thought i had no reason to question it and it was something i was doing – Jonathan Buzaglo Feb 09 '13 at 23:37
  • Another workaround it to pass an lvalue, i.e. `int x = 0;` and then pass `x` instead. However, having references for built-in types is quite pointless, you should tell your professor that the signature should be changed. – Jesse Good Feb 09 '13 at 23:39
  • @JonathanBuzaglo you'd be surprised how much bad things is tried to be teached regarding C++ :( – Bartek Banachewicz Feb 10 '13 at 00:03
0

The compiler is correct. You are calling maxSubsequenceSum1(std::vector<int>&, int, int), you defined maxSubsequenceSum1(std::vector<int>&, int &, int &)

There are 2 quick solutions:

1) Redefine your function to not take a reference.
2) Move your constants to variables and pass them along that way.

Note: there is another problem with your code. You invoke the function maxSubsequenceSum1, but you do not tell it what template parameter to use.

I have been corrected, and the correction is correct. The note is not valid.

David D
  • 1,571
  • 11
  • 12
0

You have declared a function that expects two integer references but the one you are calling takes two integers by value. It should be like this

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;