泛型函数中与运算符 == 不匹配
我有下面的代码,我正在尝试编写一个通用函数,它接受 2 个迭代器和一个对象,并检查是否出现任何情况并返回出现次数。
在我的简单类
class person{
string name;
int age;
public:
person(string n, int a): name(n), age(a) {}
bool operator==(person &p);
};
bool person::operator==(person &p){
return (name == p.name && age == p.age);
}
下面是我的主要通用函数
template<typename Iter, typename Obj>
int count_obj(Iter iter1, Iter iter2, Obj &obj){
int count = 0;
for (; iter1 != iter2; iter1++){
if((*iter1) == obj)
count += 1;
}
return count;
}
:
int main(){
vector<person *> myp;
person a("ted", 21); person b("sun", 100); person c("ted", 21);
myp.push_back(&a);myp.push_back(&b);myp.push_back(&c);
cout<< "occurences for person objects " << count_obj(myp.begin(), myp.end(), a) << '\n';
}
完整错误
3b.cc: In function ‘int count_obj(Iter, Iter, Obj&) [with Iter = __gnu_cxx::__normal_iterator<person**, std::vector<person*> >, Obj = person]’:
3b.cc:61:79: instantiated from here
3b.cc:42:3: error: no match for ‘operator==’ in ‘iter1.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = person**, _Container = std::vector<person*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = person*&]() == obj’
make: *** [3b] Error 1
我似乎无法弄清楚我收到了这个错误。
I have the code below, I'm attempting to write a generic function which takes 2 iterators and an object and checks for any occurrences and returns the number of occurrences.
below my simple class
class person{
string name;
int age;
public:
person(string n, int a): name(n), age(a) {}
bool operator==(person &p);
};
bool person::operator==(person &p){
return (name == p.name && age == p.age);
}
Below is the generic function
template<typename Iter, typename Obj>
int count_obj(Iter iter1, Iter iter2, Obj &obj){
int count = 0;
for (; iter1 != iter2; iter1++){
if((*iter1) == obj)
count += 1;
}
return count;
}
my main:
int main(){
vector<person *> myp;
person a("ted", 21); person b("sun", 100); person c("ted", 21);
myp.push_back(&a);myp.push_back(&b);myp.push_back(&c);
cout<< "occurences for person objects " << count_obj(myp.begin(), myp.end(), a) << '\n';
}
Full error
3b.cc: In function ‘int count_obj(Iter, Iter, Obj&) [with Iter = __gnu_cxx::__normal_iterator<person**, std::vector<person*> >, Obj = person]’:
3b.cc:61:79: instantiated from here
3b.cc:42:3: error: no match for ‘operator==’ in ‘iter1.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = person**, _Container = std::vector<person*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = person*&]() == obj’
make: *** [3b] Error 1
I cant seem to figure out I'm getting this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个
person *
向量,并且您尝试将它们与person
进行比较。您需要将count_obj
中的代码行修改为:或:,
具体取决于您希望比较指针还是对象。
[注意:您是否了解
std::count
标准库中的函数?][注意(2):正如另一个答案中提到的,您可能应该阅读“const 正确性”。您应该将
operator==
声明为const
,并且它应该采用const
引用作为参数。][注意(3):将原始指针存储在容器中通常是一个坏主意。例如,您是否意识到您实际上存在内存泄漏?]
You have a vector of
person *
, and you're trying to compare them against aperson
. You will need to modify the line of code incount_obj
to be either:or:
depending on whether you wish to compare pointers or objects.
[Note: Are you aware of the
std::count
function in the standard library?][Note (2): As mentioned in another answer, you should probably read up on "const correctness". You should declare your
operator==
asconst
, and it should take aconst
reference as an argument.][Note (3): Storing raw pointers in a container is often a bad idea. For instance, are you aware that you effectively have a memory leak?]