QTest fails to compile when in QCOMPARE use classes under some namespace
-
wrote on 30 Aug 2019, 20:56 last edited by aha_1980
Lets say we have:
namespace MyNamespace { class MyClass { public : QString a; }; }
and in test unit:
.... #include "myclass.h" using namespace MyNamespace; .... test_case1 { MyClass myClass; myClass.a = "test"; MyClass myClass2; myClass2.a = "test"; QCOMPARE(myClass, myClass2); //Fails to compile } operator==(const MyClass &class1, MyClass &class2) { return (class1.a == class2.a); }
it fails in QCOMPARE
error: no match for ‘operator==’ (operand types are ‘const MyNamespace MyClass’ and ‘const MyNamespace MyClass’)
some idea?
-
wrote on 31 Aug 2019, 17:21 last edited by
I solved by wrapping the definition of operator == into std namespace
namespace std { bool operator==(const MyNamespace::MyClass class1, const MyNamespace::MyClass class2) { return (class1.a == class2.a); } }
-
Hi
did you try to make both parameters for the operator const ?
The signature is
bool operator==(const X& lhs, const X& rhs) -
Hi
did you try to make both parameters for the operator const ?
The signature is
bool operator==(const X& lhs, const X& rhs)wrote on 31 Aug 2019, 15:22 last edited by@mrjj yes, both are const
bool operator==(const MyNamespace::MyClass &class1, const MyNamespace::Myclass &class2) { return (class1.a == class2.a); }
without using namespace works as expected.
-
Hi,
Can you show the complete declaration of your class and operator ?
-
wrote on 31 Aug 2019, 17:21 last edited by
I solved by wrapping the definition of operator == into std namespace
namespace std { bool operator==(const MyNamespace::MyClass class1, const MyNamespace::MyClass class2) { return (class1.a == class2.a); } }
1/5