I'm new to C++ with some experience in C, and to learn it I got myself testing some things while doing my homework. Now, I have
#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;
}
From this piece of code, I can say
- I read that time(0) could change some result, but didn't change nothing.
- yes, I would like to get a Many object to create its Object vector when instantiated, with the vector initializing all of its MAX_OBJS elements x attribute with value n.
- I was trying to make some code to find a given element, in this case the last one, in some vector (in this case, the same where the element is). I tried a couple things like find and find_if, without success.
I would like some advice/help with my problems, which are
- Line A) prints same number (the random one) MAX_OBJS times
- I don't know a better way to compare objects than I did in B)
- Line C) never prints out "Found!"
find doesn't work, even with @Nawaz suggestion. Compiler says:
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘RandomAccessIterator std::_find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >, _Tp = Object]’:
/usr/include/c++/4.5/bits/stl_algo.h:4209:45: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator >, _Tp = Object]’
../src/Many.cpp:48:74: instantiated from here
/usr/include/c++/4.5/bits/stl_algo.h:158:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:4209:45: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator >, _Tp = Object]’
../src/Many.cpp:48:74: instantiated from here
/usr/include/c++/4.5/bits/stl_algo.h:162:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:166:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:170:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:178:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:182:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
/usr/include/c++/4.5/bits/stl_algo.h:186:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Object& == __val’
Thanks!
Many
, you use bothMAX_OBJS
andobjs
, but I don't see a definition for either. Likewise in main, you definei
but never seem to use it, but usej
without ever defining it. You also userand
as both a data member and a function inside of Object, which is confusing at best. – Jerry Coffin