Friday, 15 June 2012

c++ - Why does QCOMPARE(QString("1"), "1") cause a linker error? -


I am looking for the unit testing frame of QT, and I saw that one strange thing - given that QString already applies the equality operator to constant four * , I hope. QCOMPARE (QString ("1", "1") to work only, but instead it is a linky error:

  tst_untitled14test.obj: -1: Error: LNK2019: Unsecured External Symbol "bool __cdecl QTest :: qCompare & LT; Class QString, four constant [2]> (Class QString constants & amp; 4, four constant (& amp;) [2], Four constants *, four constant *, four constant *, integer) "(?? $ qCompare @ VQString @@ $$ BY01 $$ CBD @ QTest @@YA_NABVQString @@ AAY01 $$ CBDPBD22H @Z) In the referenced function" Private: Zero __thiscall Untitled14Test :: testCase1 (zero) "(testCase1 @ Untitled14Test @@AAE XXZ?)  

Example code:

  QVERIFY (Qestring ("1") == "1"); // this work QCOMPARE (QString ("1"), Qestring ("1"); // this work // QCOMPARE (QString ("1"), "1"); // causes a linker error!  

Why is that so? Does QCOMPARE do not use equality operator of 2 terms?

Edit: Since it was said in the comments, this project is created by Qt, the Creator's Unit Test Wizard (File-> New Project-> Other Project-> QT Unit test), it is definitely set up properly, and QT + = testlib is included.

from QT

QCOMPARE is very strict on data types. Both actual and required are of the same type, otherwise the test will not compile. This prevents unintended behavior from presenting; That behavior is usually when the compiler is putting an indirect argument.

Looks like QCOMPARE

in the source code
  #define QCOMPARE (actual, expected) \ {if (! QTest :: qCompare (actual, expected, #actual, #expected, __FILE__, __LINE __)) \ return; \} While (0) template & lt; Typename T & gt; Inline Bull qCompare (T const & T1, T const & T2, const char * real, const char * required, const char * file, int row) {return comparison_helper (t1 == t2, "comparison value Not equal ", toasting (T1), toasting (T2), real, expected, file, line); }  

This is a template that requires the first and second argument of the same type.


No comments:

Post a Comment