consider the flowing code: struct A { static const int i = 0; }; void foo(int) { } void bar(const int &) { } int main() { foo(A::i); bar(A::i); return 0; } when compiling with -O0 option, g++ reports that: undefined reference to `A::i', but it will be ok if i use -O3 option. gcc -v says: Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)
You aren't defining anywhere A::i, just add, after struct A: const int A::i; and things will be fine. *** This bug has been marked as a duplicate of 42101 ***
(In reply to comment #1) > You aren't defining anywhere A::i, just add, after struct A: > > const int A::i; > > and things will be fine. > > *** This bug has been marked as a duplicate of 42101 *** > thanks for your reply. i found that no errors will be reported if i delete the line `bar(A::i)', so is that a bug? in fact the original code is like: template <int> class A { static const int i = -1; } template <> class A<0> { static const int i = 0; } //and some other specializations... and i don't want to write lots of "const int A<N>::i"...
(In reply to comment #2) > and i don't want to write lots of "const int A<N>::i"... You have to, your code is not valid if you use A::i in the program but it has no definition. This is not a compiler bug. *** This bug has been marked as a duplicate of 42101 ***
(In reply to comment #3) > (In reply to comment #2) > > and i don't want to write lots of "const int A<N>::i"... > > You have to, your code is not valid if you use A::i in the program but it has > no definition. This is not a compiler bug. Actually you don't have to write lots, just one: template<int N> const A<N>::i;
(In reply to comment #4) > (In reply to comment #3) > > (In reply to comment #2) > > > and i don't want to write lots of "const int A<N>::i"... > > > > You have to, your code is not valid if you use A::i in the program but it has > > no definition. This is not a compiler bug. > > Actually you don't have to write lots, just one: > > template<int N> const A<N>::i; > I tried so, but it seems do not work, could you please explain more detailedly? thx~
(In reply to comment #5) > > > > template<int N> const A<N>::i; > > > > I tried so, but it seems do not work, could you please explain more detailedly? > thx~ I missed the "int" out: template<int N> const int A<N>::i;
(In reply to comment #6) > (In reply to comment #5) > > > > > > template<int N> const A<N>::i; > > > > > > > I tried so, but it seems do not work, could you please explain more detailedly? > > thx~ > > I missed the "int" out: > > template<int N> const int A<N>::i; > thanks for your help, but it still can not be compiled under gcc 4.4.1
Then show here exactly what you are trying to compile. Note: this is *not* gcc-help.
(In reply to comment #8) > Then show here exactly what you are trying to compile. Note: this is *not* > gcc-help. > alright, take the flowing code as an example: template<int> struct A { static const int i = -1; }; template<> struct A<0> { static const int i = 0; }; template<> struct A<1> { static const int i = 1; }; template <int N> const int A<N>::i; int foo(int) { } int bar(const int &) { } int main() { foo(A<1>::i); //ok here bar(A<0>::i); //g++ complains undefined reference to `A<0>::i' return 0; }
Since you have specializations for A, you also need, in general, the corresponding definitions: const int A<0>::i; const int A<1>::i;