开发者

C++ rand doesn't work with attribute initializer

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

  1. I read that time(0) could change some result, but didn't change nothing.
  2. 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.
  3. 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

  1. Line A) prints same number (the random one) MAX_OBJS times
  2. I don't know a better way to compare objects than I did in B)
  3. 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 = 开发者_如何学Pythonstd::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!


if (&(*j) == &t)

You're comparing the addresses of the objects in the if condition above. But the following line makes a copy of the original object:

Object t = many.obj_list.back();

A copy means t is not the same object from the list. Hence your program would never print Found!.

I think you need to write this as:

Object & t = many.obj_list.back();
    // ^ note this!

It stores the reference of the object. It doesn't make a copy of the original object in the obj_list.


By the way, why don't you use std::find from <algorithm> header file?

#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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜