You are not logged in.
Hello I have a problem to compile the following code. It seems to me strange why the compiler does not accept my code:
bool operator==(const std::shared_ptr<glm::vec3> &v0, const glm::vec3 v1) {
return *v0 == v1;
}
bool operator==(const glm::vec3 v0, const std::shared_ptr<glm::vec3> &v1) {
return v0 == *v1;
}
TEST (smartPointersAndOperators, OperatorEquals2) {
std::vector<std::shared_ptr<glm::vec3>> allAs {
std::make_shared<glm::vec3>(),
std::make_shared<glm::vec3>(),
std::make_shared<glm::vec3>()};
glm::vec3 obj;
auto it = std::find(allAs.cbegin(), allAs.cend(), obj);
}
The compile error is
n file included from /home/program/test/C++11/c++11_Features_main.cpp:8:
In file included from /usr/include/gtest/gtest.h:55:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/ios:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/char_traits.h:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algobase.h:71:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/predefined_ops.h:194:17: error: invalid operands to binary expression ('const std::shared_ptr<glm::tvec3<float,
glm::precision::packed_highp> >' and 'const glm::tvec3<float, glm::precision::packed_highp>')
{ return *__it == _M_value; }
~~~~~ ^ ~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:120:8: note: in instantiation of function template specialization
'__gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float, glm::precision::packed_highp> >::operator()<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float,
glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float,
glm::precision::packed_highp> > > > > >' requested here
if (__pred(__first))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:161:14: note: in instantiation of function template specialization
'std::__find_if<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float,
glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > > > >, __gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float,
glm::precision::packed_highp> > >' requested here
return __find_if(__first, __last, __pred,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:3790:19: note: in instantiation of function template specialization
'std::__find_if<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float,
glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > > > >, __gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float,
glm::precision::packed_highp> > >' requested here
return std::__find_if(__first, __last,
^
Please, I need help...
Last edited by lnxsurf (2017-03-08 17:43:00)
Offline
Next time, please post a minimal example with headers and a main function for others to test your code. Posting error messages without the matching code and line numbers just makes it harder for others to help.
Searching for "std::find" and "no match for ‘operator==’" quickly turns up a relevant Stack Overflow thread: http://stackoverflow.com/questions/7287 … y-operator
Related:
http://www.cplusplus.com/reference/algorithm/find_if/
http://en.cppreference.com/w/cpp/language/extending_std
Working example using find_if:
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <glm/glm.hpp>
// http://stackoverflow.com/questions/7287224/why-isnt-stdfind-using-my-operator
// http://www.cplusplus.com/reference/algorithm/find_if/
// http://en.cppreference.com/w/cpp/language/extending_std
using namespace std;
bool
operator ==(
const std::shared_ptr<glm::vec3> &v0,
const glm::vec3 v1
)
{
return *v0 == v1;
}
bool
operator ==(
const glm::vec3 v0,
const std::shared_ptr<glm::vec3> &v1
)
{
return v0 == *v1;
}
struct VecFinder
{
VecFinder(const glm::vec3 & obj) : o(obj) {}
const glm::vec3 o;
bool operator()(const std::shared_ptr<glm::vec3> & lhs) const { return *lhs == o; }
};
int
main() {
glm::vec3 obj {0};
std::vector<std::shared_ptr<glm::vec3>> allAs {
std::make_shared<glm::vec3>(),
std::make_shared<glm::vec3>(obj),
std::make_shared<glm::vec3>()
};
auto it = std::find_if(allAs.cbegin(), allAs.cend(), VecFinder(obj));
std::cout << ((*it == obj) ? "found it" : "found nothing") << std::endl;
}
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Thanks a lot.
Offline