|
From: nils.hjelte on 7 May 2008 02:43 Hi! Not really sure where to post this, maybe the gcc list instead? Anyway, if I have two cpp files with identical class declarations (but with different semantics) of an internal class, and link them together, one of the implementations get thrown out without any kind of warning. Simple example: foo.cpp ========================================= #include <iostream> class DuplicateClass { public: void foo() { std::cout << "foo" << std::endl; } void foobar() { std::cout << "first foobar" << std::endl; } }; void foo() { DuplicateClass test; test.foo(); test.foobar(); } bar.cpp ========================================= #include <iostream> class DuplicateClass { public: void bar() { std::cout << "bar" << std::endl; } void foobar() { std::cout << "second foobar" << std::endl; } }; void bar() { DuplicateClass test; test.bar(); test.foobar(); } main.cpp ========================================== void foo(); void bar(); int main (int argc, char const *argv[]) { foo(); bar(); return 0; } Compiled with g++ -o test main.cpp foo.cpp bar.cpp -Wall I get the following output when executing the program foo first foobar bar first foobar The test.foobar() call in bar.cpp should output 'second foobar' not 'first foobar'. Looking at the symbols in the executable file it is obvious that one of the colliding symbols have been thrown out (only one foobar entry): $ nm test | grep DuplicateClass 00001c96 T __ZN14DuplicateClass3barEv 00001bce T __ZN14DuplicateClass3fooEv 00001c12 T __ZN14DuplicateClass6foobarEv Why won't the compiler/linker give me a warning/error when doing this? I had a really nasty bug resulting from this, which was pretty difficult to track down. I tried declaring the classes with 'static class' thinking it would work as 'static function' does in C, but it is obviously not valid C++ syntax. Is it not possible to limit the scope of the class to the local cpp file? And why does not the compiler provide a warning when this happens?? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: wide codecvt not used unless sync_with_stdio(false) Next: Adapting code for C++98 compliance. |