Why does not gtest see the definition of ==?

drsealks

I have a templated class Matrix:

    template<typename T>
    class Matrix {
        //blah-blah-blah
        }

And the following operator:

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;
}

The aforementioned operator is defined outside the Matrixes namespace.

I have a few tests(I am using framework Google Tests). However, if I write something like:

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);
}

everything works fine. Note, that I'm using operator== "manually" here:

 bool t = matrix_out_fact == matrix_out_ref;
 EXPECT_EQ(t, true);

However, if instead of these two lines I write something like:

EXPECT_EQ(matrix_ou_fact, matrix_out_ref);

I get the compilation error:

/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) {

Why doesn't gtest "see" the definition of operator==?

Thanks

StoryTeller - Unslander Monica

The comparison inside EXPECT_EQ happens in a different scope than your immediate test case. It looks up the operator function it needs to call via argument dependent lookup(ADL). Because your operator function is not in the same namespace as your class, it is not picked up by ADL.

It works inside your immediate test case because you probably include the appropriate headers in the appropriate order, so that finding the operator does not rely on ADL. But the implementation of the Gtest framework has to rely on ADL.

So the fix is easy. Move your custom operator into the Matrixes namespace. It's part of your class's public interface, so it belongs in the same namespace anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this definition returns a function?

Why DistinctBy does not contain a definition?

Why does my variable not work, but the definition does?

Why does this point free definition not work in Haskell?

Why does the definition of constructor make different value

Why CloudBlobClient does not contain a definition for GetBlockBlobReference?

Why does my Promise definition gets executed?

Why does this variable definition imply static lifetime?

Why does the quote be used before definition in Golang

Why does ABAP divide classes into implementation and definition?

Why does this class definition result in a NoMethodError?

Why does Windows not see python.exe?

Why getAnnotatedParameterTypes does not see annotations for array type?

Why does gcloud not see my managed notebook?

Why Java does not see that Integers are equal?

Why does React see the old state in this example?

Why does Android 8.1 not see resources?

Why does decltype not see the member declaration?

Why does bazel not see includes defined in bazelrc?

WebStorm does not see a git .exe. Why?

Why does the compiler not see the default code in a protocol?

Why gRPC Python does not see first field?

Why does the Discord bot not see the user?

Why does Docusign not see my redirect URI?

Can't see why it does not work

Why does Maven not see JavaFX graphics module (while Eclipse does)

why does tar . see hidden files but ls . does not?

Why does the `Binding<Bool>` in XUIView does not see the updated value?

Why does the conversion operator need a definition of the class converted to?