gpt4 book ai didi

c++ - "No matching function for call to"模板 C++

转载 作者:太空狗 更新时间:2023-10-29 21:46:34 25 4
gpt4 key购买 nike

帮助 我不明白为什么我不能运行这段用于家庭作业的代码片段,而且当 xCode 说我没有定义该函数时,它似乎不同意我的看法。请参阅下面的主要错误

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

最佳答案

更改签名

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

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

如果您执行 int & i = 0;,也会出现同样的问题。您不能从右值初始化非常量引用。 0127 是在表达式末尾过期的临时对象,临时对象不能绑定(bind)到非常量引用。

关于c++ - "No matching function for call to"模板 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14792796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com