Menu

CPPUNIT_ASSERT_EQUAL and static class members

Help
2007-09-27
2013-04-22
  • Petr Matousek

    Petr Matousek - 2007-09-27

    Hello,

    I encounter following problem: Within a test method I want to use CPPUNIT_ASSERT_EQUAL macro and pass a static const to it as expected parameter:

    class MyTest : public CppUnit::TestFixture {

      static const int testVal = 123;

      void testSomething() {
        // ...
        CPPUNIT_ASSERT_EQUAL(testVal, obj.getValue());
      }
    }

    Unfortunately this does work. GCC compiles this source without any error, but linker ends with error: undefined reference to MyTest::testVal.

    Does not anybody know, whats wrong here? CPPUNIT_ASSERT(testVal == obj.getValue()) works fine, or there is another workaround using local variable within the testSomething() method. But why it is not possible to use CPPUNIT_ASSERT_EQUAL()?

     
    • Code Guru

      Code Guru - 2007-10-05

      I'm not entirely sure why you get the linker error, but I have a few questions to help figure it out:

      Can you describe the file structure of the code you use here?  What file and directory is this MyTest class in and where is the class for the obj variable referenced in your code snippet?

      Also how are you compiling and linking the files in this example project?  Can you give the exact command-line you are using?

       
    • HRD

      HRD - 2007-10-10

      File test.cc:
      class MyTest ... {
      ...
      };

      const int MyTest::testVal; // missing

      > nm -C test|grep testVal
      ... R MyTest::testVal

       
    • Daniel

      Daniel - 2007-11-16

      You may be running into a situation where the compiler 'optimized' testVal out of existence.  Try building with no optimizations (i.e. -O0 ) capital letter O followed by a zero.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.