Creating a separate constructor

I have a class that inherits a class like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ParentClass.h
template <typename T>
Class ParentClass{

ParentClass();
virtual ~ParentClass;
...
};

// ChildClass.h
template <typename T>
Class ChildClass : public ParentClass<T>{
    // is a default constructor necessary here or does the
    // ParentClass() constructor get called automatically? Destructor?
    ChildClass(string str);  // this is the constructor I want to make

};


Whenever I try instantiating a new object of ChildClass...
1
2
3
4
5
6
7
8
// mainclass.cpp
#include "ChildClass.h"

bool mainclass::doStuff() const{
    some_string = "my string";
    childClass<string> childclass(some_string);
...
}


I get a linker error. When I comment out the attempt at instantiating the child class object, the errors go away. What am I doing wrong?
looks like you declaring your template classes as pure header classes -ie definitions with no implementations.

with templates however one makes ones life much simpler by implementing methods of class within header file.

if however you do experiment by seperating your header and implementations in seperate [.h] and [.cpp] files then you will have to include the cpp files in order to use the template.

this however is incorrect and is only mentioned for you to experiment with to get and idea of why the linker fails ... you will notice that by not including the cpp files the compiler was not able to generate the templatized implementations.
Post your exact error you get, is mainclass the same class as ParentClass? or is it a completely different class.
Topic archived. No new replies allowed.