C ++语句编译器错误

时间:2011-05-28 17:48:50

标签: c++ iterator

我的程序中有一个声明,它对两个向量的元素进行比较

 if(!*(it2+3).compare(*(lines_in_file.begin())))

我得到的编译器错误是:

test_file.cpp:140: error: 'class __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >' has no member named 'compare'

it2的类型是:

vector<std::string>::iterator it2=rec_vec.begin();

lines_in_file类型为:

vector<std::string> lines_in_file=split(argv[2],',');

拆分函数声明是:

std::vector<std::string> split(const std::string &s, char delim)

我有点困惑。已经花了很多时间思考。 请帮忙吗?

4 个答案:

答案 0 :(得分:4)

问题在于操作员“。”具有更高的优先级“*”所以这应该解决问题。

if(!(*(it2+3)).compare(*(lines_in_file.begin())))

答案 1 :(得分:4)

这是因为.运算符has higher precedence而不是*运算符。使用此:

if(!(it2+3)->compare(*(lines_in_file.begin())))

或者

if(!(*(it2+3)).compare(*(lines_in_file.begin())))

(相同)

答案 2 :(得分:1)

成员访问运算符(.)的优先级是间接运算符higher than the precedence*)。所以你的代码被解释为:

if(!*( (it2+3).compare( *(lines_in_file.begin()) ) ))

因此错误。 (为清楚起见,增加了额外的空格)

所以修复就是这样:

if(! ( *(it2+3) ).compare( *(lines_in_file.begin()) ))

答案 3 :(得分:1)

*运算符应用于

的结果
(it2+3).compare(*(lines_in_file.begin()))

这不是你想要的。只需使用():

(*(it2+3)).compare(*(lines_in_file.begin()))