0

I try to call a static function inside a templated class.

Why this code does not compile and gives the following linker error?

$ g++ test.cpp -std=c++11

/tmp/ccApaHzG.o: In function A::init(): test.cpp:(.text._ZN1AIdE4initEv[_ZN1AIdE4initEv]+0xc): undefined reference to A::val' collect2: error: ld returned 1 exit status

template<typename T>
class A
{
public:
    static T val;
    static void init()
    {
        val=0;
    }
};

int main()
{
    // A::init();
    A<double>::init();
    return 0;
}

Edit

GCC version: g++ -v

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) 

Note: this code is fine from class aspect. The problem is when it is called from main function.

UPDATE

This code has no relevance to the linked question. even such a code does not work:

template<typename T>
class A
{
public:
    static int val;
    static void init()
    {
        val=0;
    }
};

int main()
{
    A<double>::init();
    return 0;
}
barej
  • 1,330
  • 3
  • 25
  • 56
  • You should try to compile with `g++ -std=c++11 -Wall -Wextra -g test.cpp -o myprog` and you should edit your question to tell us the version of [GCC](http://gcc.gnu.org/) that you are using (try `g++ -v` to find out). It should be some 4.9 (e.g. 4.9.2) at least, since C++11 is a recent standard. – Basile Starynkevitch Jan 24 '15 at 11:03
  • Do you really think it has anything to do with templates? Have you taken any steps to prove that to yourself? – juanchopanza Jan 24 '15 at 11:05
  • 1
    @πάντα ῥεῖ : there is a slight difference with the question marked as duplicate : here the class is templated, so the syntax would be `template T A::val;` for the definition of the static variable (which may not be evident to OP ...) – Serge Ballesta Jan 24 '15 at 11:20
  • It is pointless to add offensive sentences in question ! If you think the duplicate does not answer **you** problem, simply say why. BTW would my previous comment be relevant for you question ? – Serge Ballesta Jan 24 '15 at 11:25
  • @SergeBallesta, thank you for your comment. I named no one. And I said nothing offensive. Your comment great. For updating my question, when the linked question absolutely has no relevance, how explain it in my question that my question is different? – barej Jan 24 '15 at 11:28
  • You do have a point about the duplicate being wrong. I found a better one, please have a look at it. I hope that helps. It may be better to remove the rant from your question though. – juanchopanza Jan 24 '15 at 11:28
  • @juanchopanza, Thank you. Hopefully, that guy revoked his duplication flag. Anyway, these question are just about defining the function. my code has problem when init is called from main function. – barej Jan 24 '15 at 11:34
  • @juanchopanza please see the update too. – barej Jan 24 '15 at 11:45
  • 1
    That last duplicate is correct. The error occurs when you call `init` in main, but the cause is that you are lacking the definition of the static field. But I add a new answer for your problem [here](http://stackoverflow.com/a/28125657/3545273) – Serge Ballesta Jan 24 '15 at 12:55

0 Answers0