drs*_*lks 2 c++ templates googletest
我有一个模板类Matrix:
template<typename T>\n class Matrix {\n //blah-blah-blah\n }\nRun Code Online (Sandbox Code Playgroud)\n\n以及以下运算符:
\n\ntemplate<typename T>\nbool operator==(const Matrixes::Matrix<T> &lhs, const Matrixes::Matrix<T> &rhs) {\n if (lhs.get_cols()!=rhs.get_cols() || lhs.get_rows()!=rhs.get_rows()){\n return false;\n }\n for (int r = 0; r < lhs.get_rows(); ++r) {\n for (int c = 0; c < lhs.get_cols(); ++c) {\n if (lhs.get(r, c) != rhs.get(r, c)) {\n return false;\n }\n }\n\n }\n return true;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n上述运算符是在Matrixes命名空间之外定义的。
我有一些测试(我正在使用框架 Google Tests)。但是,如果我写这样的东西:
\n\nTEST(MatrixOperations, MatrixMultiplicationSimple) {\n Matrixes::Primitives<int>::VectorMatrix vec1{{{8, 3, 5, 3, 8}, {5, 2, 0, 5, 8}, {0, 3, 8, 8, 1}, {3, 0, 0, 5, 0}, {2, 7, 5, 9, 0}}};\n Matrixes::Primitives<int>::VectorMatrix vec2{{{3, 1, 7, 2, 9}, {4, 6, 2, 4, 5}, {2, 5, 9, 4, 6}, {5, 3, 3, 1, 2}, {1, 8, 2, 6, 8}}};\n Matrixes::Matrix<int> vec1m{vec1};\n Matrixes::Matrix<int> vec2m{vec2};\n Matrixes::Matrix<int> matrix_out_ref{{{69, 124, 132, 99, 187}, {56, 96, 70, 71, 129}, {69, 90, 104, 58, 87}, {34, 18, 36, 11, 37}, {89, 96, 100, 61, 101}}};\n Matrixes::Matrix<int> matrix_out_fact = vec1m * vec2m;\n bool t = matrix_out_fact == matrix_out_ref;\n EXPECT_EQ(t, true);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n一切正常。请注意,我operator==在这里使用“手动”:
bool t = matrix_out_fact == matrix_out_ref;\n EXPECT_EQ(t, true);\nRun Code Online (Sandbox Code Playgroud)\n\n但是,如果我不写这两行,而是写如下内容:
\n\nEXPECT_EQ(matrix_ou_fact, matrix_out_ref);\nRun Code Online (Sandbox Code Playgroud)\n\n我收到编译错误:
\n\n/usr/local/include/gtest/gtest.h:1522:11: error: no match for \xe2\x80\x98operator==\xe2\x80\x99 (operand types are \xe2\x80\x98const Matrixes::Matrix<int>\xe2\x80\x99 and \xe2\x80\x98const Matrixes::Matrix<int>\xe2\x80\x99)\n if (lhs == rhs) {\nRun Code Online (Sandbox Code Playgroud)\n\n为什么看不到gtest的定义operator==?
谢谢
\n| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |