"Undefined Reference to..." in Template Class

Good Evening,

I have two template classes, cut down versions are shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// ClassA.hpp
#ifndef _ClassA_hpp_
#define _ClassA_hpp_

#include <iostream>
#include <vector>

template< class _Tp >
class A
{
     private: std::vector< _Tp > m_value;
     public: void add( const _Tp& value )
     {
          m_value.push_back(value);
     }
};

#endif

// ClassB.hpp
#ifndef _ClassB_hpp_
#define _ClassB_hpp_

#include "ClassA.hpp"

template< class _Tp >
class B
{
     private: static ClassA< B* > m_value;
     public: B()
     {
          m_value.add( this );
     }
};

#endif 


Class B is instantiated as:

 
ClassB< int >* = new ClassB< int >();


But I get the error: undefined reference to ClassB< int >::m_value in every method that references m_value. Most the examples I could on the net of this error relate to users who seperate the implementation out of the template file. I have not done this though. So the only reason I could come up with is that it might be something to do with the template instantiation such as this would fail:

1
2
ClassB< int >* = new ClassB< int >();
ClassB< std::string >* = new ClassB< std::string >(); // this would fail maybe? 


I guess my question is - am I right and if so how do I get around the issue?

Thank you for any assistance offered.

Phil.
I wasn't getting that error. I was getting different errors, which first I had to fix several of your syntax errors in that post (ClassA should be A, etc). Once I did that, the error turned out to be that the variable wasn't defined anywhere (as statics should be), and once I defined it, the program compiled fine.

1
2
3
4
//the other stuff goes above
int main() {
	B< int >* temp = new B< int >;
}
Thanks for the reply, I have just re-read my post and am appalled at how many mistakes were in it. Unfortunately they were just typos as I am not permitted to post actual code on here which would make things a whole lot easier. Class B implements a wrapper for a pointer, Class A implements a list. Both are templates declared and defined in their own files. There are no syntax errors in the actual files and the errors only happen when linking.
Good Evening,

Sorry about the post last night I was trying to post an article off my phone which obviously failed. I have conducted some tests today and essentially confirmed what I initially believed which is it is because of the template initialization. Essentially what I need to do is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

template< class _Tp >
class A
{};

template< class _Tp >
class B
{};

int main (int argc, char * const argv[]) {
	B< A* >* b = new B< A* >();     // this complains that A is missing template parameter
    return 0;
}     


So I need to be able to add a pointer to A to the list no matter what template type A has. Is this possible at all? GCC seems to complain about each different method I have tried.

Many thanks,

Phil.
¿so you want an heterogeneous container? ¿what for?
By the way B<int> temp; that is how an object is created. (in this case using the default constructor)
Class B maintains a list of all instance's of class A that are created. This would not be so much of a problem if Class A were not a template but it is and I can't do much about that. So somehow I need to figure out how to add any instance of class A to class B such that I can do this:

1
2
3
4
5
6
7
8
9
10
11
A<int>* a = new A<int>(0);     // assume there is a constructor to do this
A<string>* b = new A<string>("foo");     // and for this
// etc... for any type or custom class you like....

B< A* >* d = new B< A* >();     // creates a new list
d->add(a);
d->add(b);

// and on and on....
// for ANY type of A that is created.


That's what I'd like to do but it complains about a missing template parameter when creating the instance of B.

Thank you for your response so far.

Phil.
Topic archived. No new replies allowed.