【发布时间】:2015-10-27 14:47:51
【问题描述】:
我在使用带有 PCL 和 Google 测试 (GTest) 的自定义重载“==”运算符时遇到问题
#include <pcl/point_types.h>
namespace pcl { struct PointXYZ; }
bool operator==(pcl::PointXYZ p1, pcl::PointXYZ p2) {return p1.x-p2.x<.1;}
#include <gtest/gtest.h>
TEST(Foo, bar) {
pcl::PointXYZ a{2,3,4};
pcl::PointXYZP b{2,3,4};
EXPECT_EQ(a,b); // Compile error no match for operator==
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我得到的错误是:
|| /usr/include/gtest/gtest.h: In instantiation of 'testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = pcl::PointXYZ; T2 = pcl::PointXYZ]':
/usr/include/gtest/gtest.h|1361 col 23| required from 'static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = pcl::PointXYZ; T2 = pcl::PointXYZ; bool lhs_is_null_literal = false]'
src/foo/src/tests.cpp|20 col 3| required from here
/usr/include/gtest/gtest.h|1325 col 16| error: no match for 'operator==' (operand types are 'const pcl::PointXYZ' and 'const pcl::PointXYZ')
|| if (expected == actual) {
|| ^
/usr/include/gtest/internal/gtest-linked_ptr.h|213 col 6| note: candidate: template<class T> bool testing::internal::operator==(T*, const testing::internal::linked_ptr<T>&)
|| bool operator==(T* ptr, const linked_ptr<T>& x) {
|| ^
/usr/include/gtest/internal/gtest-linked_ptr.h|213 col 6| note: template argument deduction/substitution failed:
/usr/include/gtest/gtest.h|1325 col 16| note: mismatched types 'T*' and 'pcl::PointXYZ'
我试图坚持底漆: https://github.com/google/googletest/blob/master/googletest/docs/primer.md#binary-comparison
特别是,我的运算符是在包含 gtest 之前定义的,我确信类型匹配。我还尝试编写重载以获取 const 引用,但这只是比较地址而不是值。
【问题讨论】:
-
尝试将
operator ==移动到与PointXYZ相同的命名空间中。 -
错误表示它正在尝试比较
const pcl::PointXYZs。如果您将operator==更改为bool operator==(const pcl::PointXYZ& p1, const pcl::PointXYZ& p2) {return p1.x-p2.x<.1;},会发生什么?
标签: c++ operator-overloading googletest