Steven T. Hatton wrote:
with the results shown at the end of the code listing. The problem seems to be related to my using static const class members. Other similarly configured programs work OK. Can someone tell me what I'm doing wrong here? namespace sth { namespace util { class RgbColor : public Printable_IF { public: static const RgbColor RED; };
namespace sth { util::RgbColor::RgbColor(const unsigned char& r, const unsigned char& g, const unsigned char& b) : m_r(r) , m_g(g) , m_b(b) {}
const util::RgbColor util::RgbColor::RED = util::RgbColor(255,0,0); }
undefined reference to `sth::util::RgbColor::RED' ./sth/libsth.a(ColorTest.o)(.text+0x6f):./sth/ColorTest.cpp:9: undefined
I've seen this kind of problem before. Effectively, you're trying to initialise a static const class member at runtime (i.e. not compile time!) in a static object That doesn't work, though I can't remember why - I don't know if it should work in C++ or if it's implementation defined. There are several solutions. First, I think the code will work if you create a shared library. Similar code does for me. Perhaps a neater solution for you would be to replace the static const variables with static const functions of the form: const util::RgbColor util::RgbColor::RED(){ static util::RgbColor util::RgbColor RED = util::RgbColor(255,0,0); return RED; } That should work unless you're running the code in an insanely multithreaded environment. -- JDL