A static vector member in a template class results in undefined reference

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A static vector member in a template class results in undefined reference

Franky Braem
Hi,

I'm trying to use a vector as static member in a template class. The
following code:

#include <stdio.h>
#include <vector>

template<class T>
class Native
{
  public:
   
  static char* className;
  static std::vector<int> m_methods;
};

class Buffer : public Native<Buffer>
{
};

template<>
char* Native<Buffer>::className = "Buffer";

template<>
std::vector<int> Native<Buffer>::m_methods;

int main(int argc, char **argv)
{
  Buffer buf1;
 
  Buffer::m_methods.push_back(0);
  printf("%s", Buffer::className);
    return 0;
}

Compiles fine, but I get the following link error:

----------Build Started--------
"C:\MinGW\bin\mingw32-make.exe"  -j 1 -f "Test_wsp.mk"
----------Building project:[ Test - Debug ]----------
g++ -o ./Debug/Test ./Debug/main.o  "-Lc:/mingw/lib"  
./Debug/main.o: In function `_ZSt14__copy_trivialIiEPT_PKS0_S3_S1_':
C:/Program
Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algobase.h:259:
undefined reference to `Native<Buffer>::m_methods'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [Debug/Test] Error 1
mingw32-make: *** [All] Error 2
----------Build Ended----------
0 errors, 0 warnings


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) HTML/MIME encoded mail
3) Improper quoting
4) Improper trimming
Reply | Threaded
Open this post in threaded view
|

Re: A static vector member in a template class results in undefined reference [SOLVED]

Franky Braem
Solved by initializing the vector as follows:

template<>
std::vector<int> Native<Buffer>::m_methods = std::vector<int>();

Franky.


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) HTML/MIME encoded mail
3) Improper quoting
4) Improper trimming
Reply | Threaded
Open this post in threaded view
|

Re: A static vector member in a template class results in undefined reference

Basilevs
In reply to this post by Franky Braem
On Thu, Jan 29, 2009 at 2:49 AM, Franky Braem <[hidden email]> wrote:

> #include <stdio.h>
> #include <vector>
>
> template<class T>
> class Native
> {
>  public:
>
>  static char* className;
>  static std::vector<int> m_methods;
> };
>
> class Buffer : public Native<Buffer>
> {
> };
>
> template<>
> char* Native<Buffer>::className = "Buffer";
>
> template<>

Removing previous line does the trick. IMHO, this construct should
only be used to specialize a template explicitly while you are trying
to define a member of automatically instantiated template.

> std::vector<int> Native<Buffer>::m_methods;
>
> int main(int argc, char **argv)
> {
>  Buffer buf1;
>
>  Buffer::m_methods.push_back(0);
>  printf("%s", Buffer::className);
>    return 0;
> }
>
> Compiles fine, but I get the following link error:

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) HTML/MIME encoded mail
3) Improper quoting
4) Improper trimming