【发布时间】:2020-11-30 22:16:06
【问题描述】:
我对函数 indice_max 有一个错误“不能有 cv 限定符”,我不明白为什么,我不会随时修改向量 v
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
int indice_max(const vector<double> & v) const{
int indice =0;
double max = v[0];
for(int i=0; i<v.size(); i++){
if(v[i]>max){
max = v[i];
indice = i;
}
}
return indice;
}
【问题讨论】:
-
不能将非成员函数声明为
const。 -
int indice_max(const vector<double> & v) const第二个const声明 this 是其成员函数的类的变量在逻辑上对于该函数是恒定的。如果这 not 是一个成员函数,那就没有意义了。因此,您不能将非成员函数声明为const。 -
这可能会回答你的问题:stackoverflow.com/questions/3474119/…
标签: c++