undefined reference to static constexpr bool expectedIsEmpty

Added by Gerd Hirsch 18 days ago

hello cute team,
this is what I wanted to write:
ASSERT_EQUALM("pop()", expectedIsEmpty, buffer.pop(popValue));
ASSERT_EQUALM("pop()", !expectedIsEmpty, buffer.pop(popValue));
secund line works but first doesn´t
I tried: ASSERT_EQUALM("pop()", !!expectedIsEmpty, buffer.pop(popValue));
which works, I believe, there s a problem with the makro
Error message from Eclipse/CDT on windows 10 with Cygwin GNU Compiler collection
undefined reference to `RingBufferTest<ReaderWriterMultiThreaded<int, 3u, BlockingPolicy> >::expectedIsEmpty'

Definition in class RingBufferTest
static constexpr bool expectedIsEmpty{false};
definitions of test methods and class are all in header RingBufferTest.h

thx for support
gerd


Replies (1)

RE: undefined reference to static constexpr bool expectedIsEmpty - Added by Peter Sommerlad 15 days ago

If you declare a static member in a class you also are required to provide a definition for it, even if it is constexpr, once you need the address of it, e.g. by passing it by const reference, this gives the static data member "linkage" and the linker will need it.

using the

!expectedIsEmpty

expression will create a temporary that is then passed by const reference to our ASSERT_EQUAL machinery.

using the static member variable directly will pass that by const reference and thus requiring it to have an address.

This means a definition is missing.

In C++17 you can define the static constexpr variable as "inline", to sidestep the need for a separate definition:

static constexpr inline bool expectedIsEmpty{};

(1-1/1)