泛型函数中与运算符 == 不匹配

发布于 2024-12-22 22:48:57 字数 1476 浏览 0 评论 0原文

我有下面的代码,我正在尝试编写一个通用函数,它接受 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-12-29 22:48:57

您有一个 person * 向量,并且您尝试将它们与 person 进行比较。您需要将 count_obj 中的代码行修改为:

if (*(*iter1) == obj)

或:,

if ((*iter1) == &obj)

具体取决于您希望比较指针还是对象。

[注意:您是否了解 std::count 标准库中的函数?]

[注意(2):正如另一个答案中提到的,您可能应该阅读“const 正确性”。您应该将 operator== 声明为 const,并且它应该采用 const 引用作为参数。]

[注意(3):将原始指针存储在容器中通常是一个坏主意。例如,您是否意识到您实际上存在内存泄漏?]

You have a vector of person *, and you're trying to compare them against a person. You will need to modify the line of code in count_obj to be either:

if (*(*iter1) == obj)

or:

if ((*iter1) == &obj)

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== as const, and it should take a const 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?]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文