1

    person<worker> people (w);

I have person class and worker class, I want to create people objects from the person class.

//person.cpp
template <class T>
person<T>::~person()
{
    //dtor
}

//person.h
template <class T>
class person
{
    public:
        person();
        person(T a);
        virtual ~person();

};

I get

undefined reference to `person<worker>::person(worker).

What am I doing wrong?

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27

1 Answers1

0

Your constructor taking a worker is not defined. You only have the header, not the definition. This leads to the error

enter image description here

I added the definition in the class declaration as this:

person(T a) { x = a; };

Here is a minimal code that compiles and executes (from what you have done) :

#include <iostream>
class worker
{
 private:
    int a;
  public:
    worker(){}
    ~worker() {}
};

//person.h
template <class T>
class person
{
    public:
        person();
        person(T a) { x = a; };
        virtual ~person();

    private:
        T x;

};

//person.cpp
template <class T>
person<T>::~person()
{
    //dtor
}

int main()
{
    worker w;
    person<worker> people(w);

}

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27