为什么 gtest 看不到 == 的定义?
Why does not gtest see the definition of ==?
我有一个模板 class Matrix
:
template<typename T>
class Matrix {
//blah-blah-blah
}
以及以下运算符:
template<typename T>
bool operator==(const Matrixes::Matrix<T> &lhs, const Matrixes::Matrix<T> &rhs) {
if (lhs.get_cols()!=rhs.get_cols() || lhs.get_rows()!=rhs.get_rows()){
return false;
}
for (int r = 0; r < lhs.get_rows(); ++r) {
for (int c = 0; c < lhs.get_cols(); ++c) {
if (lhs.get(r, c) != rhs.get(r, c)) {
return false;
}
}
}
return true;
}
上述运算符是在 Matrixes
命名空间之外定义的。
我有一些测试(我正在使用框架 Google 测试)。但是,如果我这样写:
TEST(MatrixOperations, MatrixMultiplicationSimple) {
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}}};
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}}};
Matrixes::Matrix<int> vec1m{vec1};
Matrixes::Matrix<int> vec2m{vec2};
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}}};
Matrixes::Matrix<int> matrix_out_fact = vec1m * vec2m;
bool t = matrix_out_fact == matrix_out_ref;
EXPECT_EQ(t, true);
}
一切正常。请注意,我在这里使用 operator==
"manually":
bool t = matrix_out_fact == matrix_out_ref;
EXPECT_EQ(t, true);
但是,如果不是这两行,我写的是:
EXPECT_EQ(matrix_ou_fact, matrix_out_ref);
我得到编译错误:
/usr/local/include/gtest/gtest.h:1522:11: error: no match for ‘operator==’ (operand types are ‘const Matrixes::Matrix<int>’ and ‘const Matrixes::Matrix<int>’)
if (lhs == rhs) {
为什么 gtest
"see" operator==
的定义没有?
谢谢
EXPECT_EQ
中的比较发生在与您的直接测试用例不同的范围内。它通过 argument dependent lookup(ADL) 查找需要调用的运算符函数。因为您的运算符函数与您的 class 不在同一个命名空间中,所以它不会被 ADL 拾取。
它在您的直接测试用例中起作用,因为您可能以适当的顺序包含适当的 headers,这样查找运算符就不会依赖于 ADL。但是Gtest框架的实现不得不依赖ADL
所以修复很简单。将您的自定义运算符移至 Matrixes
命名空间。它是您 class 的 public 接口的一部分,因此无论如何它都属于同一个命名空间。
我有一个模板 class Matrix
:
template<typename T>
class Matrix {
//blah-blah-blah
}
以及以下运算符:
template<typename T>
bool operator==(const Matrixes::Matrix<T> &lhs, const Matrixes::Matrix<T> &rhs) {
if (lhs.get_cols()!=rhs.get_cols() || lhs.get_rows()!=rhs.get_rows()){
return false;
}
for (int r = 0; r < lhs.get_rows(); ++r) {
for (int c = 0; c < lhs.get_cols(); ++c) {
if (lhs.get(r, c) != rhs.get(r, c)) {
return false;
}
}
}
return true;
}
上述运算符是在 Matrixes
命名空间之外定义的。
我有一些测试(我正在使用框架 Google 测试)。但是,如果我这样写:
TEST(MatrixOperations, MatrixMultiplicationSimple) {
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}}};
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}}};
Matrixes::Matrix<int> vec1m{vec1};
Matrixes::Matrix<int> vec2m{vec2};
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}}};
Matrixes::Matrix<int> matrix_out_fact = vec1m * vec2m;
bool t = matrix_out_fact == matrix_out_ref;
EXPECT_EQ(t, true);
}
一切正常。请注意,我在这里使用 operator==
"manually":
bool t = matrix_out_fact == matrix_out_ref;
EXPECT_EQ(t, true);
但是,如果不是这两行,我写的是:
EXPECT_EQ(matrix_ou_fact, matrix_out_ref);
我得到编译错误:
/usr/local/include/gtest/gtest.h:1522:11: error: no match for ‘operator==’ (operand types are ‘const Matrixes::Matrix<int>’ and ‘const Matrixes::Matrix<int>’)
if (lhs == rhs) {
为什么 gtest
"see" operator==
的定义没有?
谢谢
EXPECT_EQ
中的比较发生在与您的直接测试用例不同的范围内。它通过 argument dependent lookup(ADL) 查找需要调用的运算符函数。因为您的运算符函数与您的 class 不在同一个命名空间中,所以它不会被 ADL 拾取。
它在您的直接测试用例中起作用,因为您可能以适当的顺序包含适当的 headers,这样查找运算符就不会依赖于 ADL。但是Gtest框架的实现不得不依赖ADL
所以修复很简单。将您的自定义运算符移至 Matrixes
命名空间。它是您 class 的 public 接口的一部分,因此无论如何它都属于同一个命名空间。