[英]C++ statement compiler error
i have a statement in my program which does a comparision of elements of a two vectors我的程序中有一个语句,它比较两个向量的元素
if(!*(it2+3).compare(*(lines_in_file.begin())))
the compiler error i am getting is:我得到的编译器错误是:
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
's type is: it2
的类型是:
vector<std::string>::iterator it2=rec_vec.begin();
lines_in_file
type is: lines_in_file
类型是:
vector<std::string> lines_in_file=split(argv[2],',');
split function declaration is:拆分 function 声明为:
std::vector<std::string> split(const std::string &s, char delim)
I am confused a bit.already spent a lot of time thinking.我有点困惑。已经花了很多时间思考。 could any one please help?有人可以帮忙吗?
The problem is that the operator "."问题是运算符“。” have greater precedence that "*" so this should solve the problem.具有比“*”更高的优先级,因此这应该可以解决问题。
if(!(*(it2+3)).compare(*(lines_in_file.begin())))
This happens because .
发生这种情况是因为.
operator has higher precedence than *
operator.运算符的优先级高于*
运算符。 Use this:用这个:
if(!(it2+3)->compare(*(lines_in_file.begin())))
or this或这个
if(!(*(it2+3)).compare(*(lines_in_file.begin())))
(which are equal) (相等)
Precedence of the member access operator ( .
) is higher than the precedence of indirection operator ( *
).成员访问运算符 ( .
)的优先级高于间接运算符 ( *
) 的优先级。 So your code is interpreted as:所以你的代码被解释为:
if(!*( (it2+3).compare( *(lines_in_file.begin()) ) ))
Hence the error.因此错误。 (extra spaces is added for clarity) (为清楚起见,添加了额外的空格)
So the fix is this:所以修复是这样的:
if(! ( *(it2+3) ).compare( *(lines_in_file.begin()) ))
THe * operator is applied to the result of * 运算符应用于结果
(it2+3).compare(*(lines_in_file.begin()))
This is not what you want.这不是你想要的。 Just use ():只需使用():
(*(it2+3)).compare(*(lines_in_file.begin()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.