“没有匹配功能用于呼叫”模板C ++ [英] "No matching function for call to" Templates C++

查看:215
本文介绍了“没有匹配功能用于呼叫”模板C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助我不明白为什么我不能运行这段代码是一个家庭作业,xCode似乎不同意我,当它说我没有定义的功能。请参阅下面的主要错误

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


推荐答案

p>

Change the signature from

Comparable maxSubsequenceSum1( const vector<Comparable> & a,
                               int & seqStart, int & seqEnd)

Comparable maxSubsequenceSum1( const vector<Comparable> & a, 
                                 int seqStart, int seqEnd)

同样的问题发生,如果你会做 int& i = 0; 。您不能从右值初始化非const引用。 0 127 是在表达式结尾处过期的临时对象,temporary不能绑定到非const引用。

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.

这篇关于“没有匹配功能用于呼叫”模板C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆