2

我显然在这里遗漏了一些东西,但是什么?

定义(...表示有效代码,不是实际的三点):

class CrmxFile {
private:
    const std::vector<std::string> validValues;
    int value;

public:
    void setValue(std::string _value);

...
}

std::vector<std::string> CrmxFile = {...};

在代码中我有这个:

void Crmx::SetValue(std::string _value) {
    std::vector<std::string>::iterator idx;
    if((idx = std::find(validValues.begin(), validValues.end(), _value)) == validValues.end()) {
        value = 0;
    }
    else {
        value = idx - validValues.begin();
    }
}

我编译这个,gcc -c -std=c++0x我得到这个错误:

CrmxFile.cpp: In member function ‘void CrmxFile::SetValue(std::string)’:
CrmxFile.cpp:24:64: error: no match for ‘operator=’ in ‘idx = std::find [with _IIter = __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >, _Tp = std::basic_string<char>](Id3V1::validValues.std::vector<_Tp, _Alloc>::begin [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_pointer = const std::basic_string<char>*](), Id3V1::validValues.std::vector<_Tp, _Alloc>::end [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_pointer = const std::basic_string<char>*](), (*(const std::basic_string<char>*)(& _value)))’
CrmxFile.cpp:24:64: note: candidates are:
/usr/include/c++/4.6/bits/stl_iterator.h:702:11: note: __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >& __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >::operator=(const __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >&)
/usr/include/c++/4.6/bits/stl_iterator.h:702:11: note:   no known conversion for argument 1 from ‘__gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >’ to ‘const __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >&’
/usr/include/c++/4.6/bits/stl_iterator.h:702:11: note: __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >& __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >::operator=(__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >&&)
/usr/include/c++/4.6/bits/stl_iterator.h:702:11: note:   no known conversion for argument 1 from ‘__gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >’ to ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >&&’

我错过了什么?或者,是否有更好的方法来查找向量中给定值的索引?

4

4 回答 4

5

idx必须是一个,const_iterator因为 validValues是一个const vector<...>

std::vector<std::string>::const_iterator idx;
于 2013-01-08T17:59:21.077 回答
1

由于validValuesis const, begin(), end(), 因此 , 的这个实例化std::find都返回std::vector<std::string>::const_iterator, not std::vector<std::string>::iterator。为了 const 安全, aconst_iterator不能转换为iterator.

于 2013-01-08T18:02:04.830 回答
1

validValues定义为const std::vector<std::string>。因此begin()andend()将返回const_iterator,但您正试图将结果分配给 a iterator。将 a 转换const_iterator为 aniterator会破坏 const 的正确性(因为您可以继续更改底层对象),因此是不允许的。

更改idx to be的定义,std::vector<std::string>::const_iterator它应该可以工作。

于 2013-01-08T18:02:23.023 回答
1

“我错过了什么?”

您的第一个 validValues 无效吗?

我建议:

void Crmx::SetValue(std::string _value) {

        value = std::find(validValues.begin(), validValues.end(), _value) - validValues.begin();

}

并使用 validValues.size 作为无效值,而不是 0

于 2013-01-08T18:41:24.990 回答