我是C +的新手,有一些C语言的经验,为了学习它,我在做作业的同时测试了一些东西。现在,我有
#define MAX_OBJS 4
using namespace std;
class Object {
public:
int x, rand;
Object(int y) {
x = y;
rand = rand() % 5;
};
};
class Many {
public:
vector<Object> obj_list;
Many(int n): obj_list (MAX_OBJS, n) {}
};
int main() {
srand(time(NULL));
Many many(42);
cout << "Example: looking for " << many.obj_list.back().rand "\n";
vector<Object>::iterator j;
Object t = many.obj_list.back();
for (j = many.obj_list.begin(); j != many.obj_list.end(); j++) {
/*A*/ cout << j->rand << "\n";
/*B*/ if (&(*j) == &t)
/*C*/ cout << "Found!" << "\n";
}
return EXIT_SUCCESS;
}
从这段代码中,我可以说
我想对我的问题提出一些建议/帮助,
查找也不起作用。编译说:
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:在函数' RandomAccessIterator std :: _find(_RandomAccessIterator,_RandomAccessIterator,const _Tp&amp;,std :: random_access_iterator_tag)[with _RandomAccessIterator = __gnu_cxx :: __ normal_iterator&gt;,_ Tp = Object]':
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:4209:45:从'_IIter std :: find(_IIter,_IIter,const _Tp&amp;)实例化[与_IIter = __gnu_cxx :: __ normal_iterator&gt;, _Tp =对象]'
../ src / Many.cpp:48:74:从这里实例化
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:158:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:4209:45:从'_IIter std :: find(_IIter,_IIter,const _Tp&amp;)实例化[与_IIter = __gnu_cxx :: __ normal_iterator&gt;, _Tp =对象]'
../ src / Many.cpp:48:74:从这里实例化
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:162:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:166:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:170:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:178:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:182:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
/ usr / include / c ++ / 4.5 / bits / stl_algo.h:186:4:错误:首先在'_ 中与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator&lt; _Iterator ,_Container&gt; :: operator * with _Iterator = Object *,_ _Container = std :: vector,__ nuu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: reference = Object&amp; == __val'
谢谢!
答案 0 :(得分:2)
if (&(*j) == &t)
您正在比较上述if
条件中对象的地址。但是以下行生成了原始对象的副本:
Object t = many.obj_list.back();
复制意味着t
与列表中的对象不同。因此,您的程序永远不会打印Found!
。
我认为你需要把它写成:
Object & t = many.obj_list.back();
// ^ note this!
它存储对象的引用。它不会复制obj_list
中的原始对象。
顺便说一句,为什么不使用std::find
头文件中的<algorithm>
?
#include <algorithm>
std::vector<Object>::iterator it = std::find(many.obj_list.begin(), many.obj_list.end(), t)
if ( it != many.obj_list.end())
std::cout << "Found!"<< std::endl;