GoogleTest无法使用自定义比较运算符
GoogleTest fails to use custom comparison operator
我想在谷歌测试中对我的Word结构进行简单的测试。为了使测试代码变得更简单、更短,我决定编写一个比较运算符(尽管我并不真的需要),并根据初级读本使用ASSERT_EQ
。尽管一切看起来都很好,但我还是收到了一个编译器错误:
/* Word.h */
namespace tgs {
struct Word {
//something here
Word();
virtual ~Word();
bool operator== (Word& rhs);
};
}
/* Word.cpp */
namespace tgs {
bool Word::operator== (Word& rhs){
return true; //there are actual member comparisons here
}
}
/* TextAreaShould_test.cc */
TEST_F(TextAreaShould, DoSomething) {
Word w1, w2;
if(w1 == w2){ //compiles and runs ok
0;
}
ASSERT_EQ(w1, w2); //produces following error
}
编译器错误:
[ 86%] Building CXX object CMakeFiles/tests/TextAreaShould.test.dir/tests/TextAreaShould_test.cc.o
/usr/bin/c++ -I/home/nietaki/zpp/TheGameShow_build -I/home/nietaki/zpp/TheGameShow -I/home/nietaki/zpp/TheGameShow/google_mock/include -I/home/nietaki/zpp/TheGameShow/google_mock/gtest/include -I/usr/local/include -o CMakeFiles/tests/TextAreaShould.test.dir/tests/TextAreaShould_test.cc.o -c /home/nietaki/zpp/TheGameShow/tests/TextAreaShould_test.cc
In file included from /home/nietaki/zpp/TheGameShow/tests/TextAreaShould_test.cc:8:
/home/nietaki/zpp/TheGameShow/google_mock/gtest/include/gtest/gtest.h: In function ‘testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = tgs::Word, T2 = tgs::Word]’:
/ home/nietaki/zpp/TheGameShow/google_mock/gtest/include/gtest/gtest.h:1363: instantiated from ‘static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = tgs::Word, T2 = tgs::Word, bool lhs_is_null_literal = false]’
/home/nietaki/zpp/TheGameShow/tests/TextAreaShould_test.cc:112: instantiated from here
/home/nietaki/zpp/TheGameShow/google_mock/gtest/include/gtest/gtest.h:1326: error: no match for ‘operator==’ in ‘expected == actual’
/home/nietaki/zpp/TheGameShow/renderer/textarea/Word.h:25: note: candidates are: bool tgs::Word::operator==(tgs::Word&)
我还尝试将比较运算符放在类之外,而不是作为成员,但这似乎没有多大区别。我试着按照初级读本做每件事,但可能出了问题。
比较运算符不会更改任何参数,因此它可以是const
,如谷歌测试所使用的:
struct Word {
Word();
virtual ~Word();
bool operator== (const Word& rhs) const;
};
一切都很顺利。
相关文章:
- C++自定义比较函数
- std::设置自定义比较器
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- 什么是自定义比较器以及如何在 C++ 的排序函数中使用它?
- 没有默认构造函数作为模板参数的自定义比较器
- 使用迭代器的自定义比较器函数
- 标准::p riority_queue 的自定义比较器背后的逻辑
- Cython中带有自定义比较器的优先级队列
- gtest 期望无序元素与自定义比较器/匹配器一起使用
- 为什么在类或结构中传递自定义比较器函数?
- 使用自定义比较函数在类内进行列表排序
- 在priority_queue中使用默认容器但自定义比较器
- 是否有任何统一的方法可以在 c++ 中创建自定义比较器?
- C++ priority_queue与自定义比较器并删除任何项目
- 如何在 C++11 中将 std::max 与自定义比较器一起使用?
- 通过函数指针对类内的 STL SET 使用自定义比较器
- 基于对象的两个属性的自定义比较器会引发异常
- 如何使用自定义比较器初始化类数据成员,该成员是 std::set