I am getting the following error when compiling my program:
1 2 3 4
main.o: In function `main':
main.cpp:(.text+0xd65): undefined reference to `Foo<A, B, C>::Foo()'
collect2: ld returned 1 exit status
make: *** [run] Error 1
The Foo class is a template class, with a template template class (i.e. Foo::B<A>), as I am using a Policy Based Design approach. Also, I have implemented all my template classes in the header files, so that isn't the cause of the issue.
In the main function the Foo class is instantiated with:
Foo < A, B, C > *foo = new Foo< A, B, C >;
The Foo class is declared as follows, with B<A> being a template template class.
1 2
template <class A, template <class> class B,class C>
class Foo {
The template member variables are declared as follows, however I don't believe they are directly causing the error, I have just included them for completeness:
1 2 3 4
private:
B< A > *pB_ ;
A *pA_;
C *pC_;
I have tried many things and just get variations of the aforementioned error message. Any help resolving this issue would be much appreciated..
This is a linker error. So, your syntax is valid (although not necessarily correct). The message seems to be saying, "I don't see the definition of the constructor, Foo(), for the type Foo<A, B, C>.
Where is Foo's default constructor defined? Can you post it?
Thanks for the response. I had some issues with a couple of my Constructors, e,g, Foo(); instead of Foo(){}. Would normally stick out in the .cpp file but working with templates in the .h made it a bit less obvious!
So I'm curious, this is valid? template <class A, template <class> class B,class C> How does the compiler know what class inside of template<class> is deduced to?
[quote =clanmjc ]So I'm curious, this is valid? template <class A, template <class> class B,class C> How does the compiler know what class inside of template<class> is deduced to? [/quote]
//a class to test with
template <typename T>
class MyClass {};
//Note the template parameter B is a Template Template parameter
//it is only a placeholder for the NAME of a template CLASS that
//takes one type parameter (like our MyClass above for example)
template <typename A, template<typename> class B, typename C>
class SomeClass
{
public:
//In this example B<int> will expand to MyClass <int> concrete class type.
SomeClass ( A a, B<int> b, C c)
{
B<std::string> bs; //Create a MyClass <std::string> object
}
};
int main ()
{
MyClass<int> mci;
//Instantiate a variable of SomeClass
//note when we specify the second template parameter we only supply the NAME
//of a suitable template class
SomeClass <int, MyClass, float> sc (3, mci, 4.1f);
}
EDITed for clarity
I should also further clarify that although we said that B is a place holder for a class name - a struct name can also be supplied (although I supposed you surmised that already)
Crystal clear, when I tried to compile the original, instead of copy pasting I wrote it by hand and forgot the class after template<class>.
So mine looked like template<class> B, which of course did not compile and thus spawned my questions... whoops!