Currently, I have a problem when trying to find a variable within a vector
.
I defined a vector
, tests
, set to contain my own structure Test
. It contains two variables, one
and two
. Both are instances of Test
.
Test
isn't much, it only contains an int
and char
.
I defined a macro that finds wether or not an instance of an object is in a given vector
or not.
When I attempt to compile my code, it results in this error:
In file included from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:71,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/char_traits.h:39,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/ios:40,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/ostream:38,
from /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/iostream:39,
from test.cpp:1:
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Value = const Test]':
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:1932:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const Test>]'
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algobase.h:1977:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const Test>]'
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/stl_algo.h:3902:28: required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Test*, std::vector<Test> >; _Tp = Test]'
test.cpp:19:6: required from here
/usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/bits/predefined_ops.h:268:17: error: no match for 'operator==' (operand types are 'Test' and 'const Test')
268 | { return *__it == _M_value; }
|
What I find interesting is that the variable I am checking is considered constant, even though when I defined it, it wasn't.
Source code:
#include <iostream>
#include <vector>
#include <algorithm>
#define isin(one, two) (std::find(one.begin(), one.end(), two) != one.end())
struct Test {
int one;
char two;
};
int main() {
Test one = { 6, 'n' };
Test two = { 9, 'r' };
std::vector<Test> tests = { one, two };
if (isin(tests, one)) {
std::cout << "one is in tests\n";
} else {
std::cout << "one is not in tests\n";
}
return 0;
}
Why is one
considered constant? Did I accidentally do something wrong?