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 361 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.
  • R Offline
    R Offline
    RonaldViscarraL
    wrote on 30 Aug 2019, 20:56 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
    • R Offline
      R Offline
      RonaldViscarraL
      wrote on 31 Aug 2019, 17:21 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 30 Aug 2019, 21:23 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)

        R 1 Reply Last reply 31 Aug 2019, 15:22
        1
        • M mrjj
          30 Aug 2019, 21:23

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

          R Offline
          R Offline
          RonaldViscarraL
          wrote on 31 Aug 2019, 15:22 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 31 Aug 2019, 15:45 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
            • R Offline
              R Offline
              RonaldViscarraL
              wrote on 31 Aug 2019, 17:21 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

              1/5

              30 Aug 2019, 20:56

              • Login

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