Reproduced on 4.6.0 and 4.1.2 as well.
Here is the header file:
class TestClass
{
public:
void testFn() const;
private:
static const int VAR1 = 9;
static const int VAR2 = 5;
};
And here is the source file including it:
#include "testclass.h"
void TestClass::testFn() const {
int b = 1;
int c = (b == 0) ? VAR1 : VAR2;
int d;
if (b == 0) d = VAR1;
else d = VAR2;
}
int main(void) { return 0; }
This compiles, but does not link (g++ is called with no options, only the
source). The following error is given:
gcc-test.o: In function `TestClass::testFn() const':
gcc-test.cpp:(.text+0x14): undefined reference to `TestClass::VAR1'
gcc-test.cpp:(.text+0x1b): undefined reference to `TestClass::VAR2'
collect2: ld returned 1 exit status
If I comment out the line "int c = (b == 0) ? VAR1 : VAR2;" in the source, then
the linker errors disappear.
I have been told that VAR1 and VAR2 are declared and initialized, but not
defined, and therefore this code is not valid C++. If it is indeed invalid,
then why does only the ternary statement cause a linking error, but not the
equivalent if/else?
--
Summary: static const variable works in if/else, fails at linking
in ternary
Product: gcc
Version: 4.4.1
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: Hodapp87 at gmail dot com
GCC build triplet: i486-linux-gnu
GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44673