Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

class - C++ unable to create template constructor with template parameter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...