The following code compiles but the linker balks at the use of one of the class
variables used within a conditional expression:

------------------------------------------------------------------------
//  simple.cpp

class BiState
{
    int state;
public:
    static const int OFF = 0,
                     ON  = 1;

    BiState(): state(OFF) {}

    //  This compiles and links
    void toggle_1() 
    {
        if ( state == OFF )
            state = ON;
        else
            state = OFF;
    }

    //  This compiles and links
    void toggle_2() 
    {
        state = (state == OFF) ? 1 : OFF; 
    }

    //  This compiles and links
    void toggle_3() 
    {
        state = (state == OFF) ? ON : 0; 
    }

    //  This compiles but does not link
    //  Linker error: BiState::ON and BiState::OFF are undefined references
    void toggle_4() 
    {
        state = (state == OFF) ? ON : OFF; 
    }
};

int main()
{
    BiState obj;

    obj.toggle_1();
    obj.toggle_2();
    obj.toggle_3();
    obj.toggle_4();
}

------------------------------------------------------------------------

Command line:
g++ simple.cpp

---
Platform:
Debian 5.0 (Lenny), standard distribution gcc 4.3.2, x86

---
g++ -v:
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1.1) 

---
Linker output:
/tmp/ccOf6GeQ.o: In function `BiState::toggle_4()':
simple.cpp:(.text._ZN7BiState8toggle_4Ev[BiState::toggle_4()]+0x10): undefined
reference to `BiState::ON'
simple.cpp:(.text._ZN7BiState8toggle_4Ev[BiState::toggle_4()]+0x1b): undefined
reference to `BiState::OFF'
collect2: ld returned 1 exit status


-- 
           Summary: Linker unable to resolve class variables used in a
                    conditional expression
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: halterman at southern dot edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42241

Reply via email to