Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTest fails to compile when in QCOMPARE use classes under some namespace
Forum Updated to NodeBB v4.3 + New Features

QTest fails to compile when in QCOMPARE use classes under some namespace

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 369 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • RonaldViscarraLR Offline
    RonaldViscarraLR Offline
    RonaldViscarraL
    wrote on last edited by aha_1980
    #1

    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?

    1 Reply Last reply
    0
    • RonaldViscarraLR Offline
      RonaldViscarraLR Offline
      RonaldViscarraL
      wrote on last edited by
      #5

      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 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        did you try to make both parameters for the operator const ?
        The signature is
        bool operator==(const X& lhs, const X& rhs)

        RonaldViscarraLR 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          did you try to make both parameters for the operator const ?
          The signature is
          bool operator==(const X& lhs, const X& rhs)

          RonaldViscarraLR Offline
          RonaldViscarraLR Offline
          RonaldViscarraL
          wrote on last edited by
          #3

          @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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            Can you show the complete declaration of your class and operator ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • RonaldViscarraLR Offline
              RonaldViscarraLR Offline
              RonaldViscarraL
              wrote on last edited by
              #5

              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 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved