【问题标题】:error LNK2019: unresolved external symbol "public: __thiscall Signal错误 LNK2019:未解析的外部符号“公共:__thiscall 信号
【发布时间】:2013-12-16 17:15:33
【问题描述】:

当我调用 main() 函数时出现错误:

错误 2 错误 LNK2019:函数“public: __thiscall Img::Img( int,int)" (??0?$Img@H@@QAE@HH@Z) c:\Users\Maja\documents\visual studio 2012\Projects\Project6\Project6\Img.obj 项目

谁能告诉我如何将 Linker 设置为不调用默认构造函数并调用我想要的构造函数?

template <class T> class Signal {

protected: int N;                               // width of array
       int M;
private:   double deltaT;                       // sampling period
       double t0;                           // initial time
           int flag;                            // indicator
public:  
       T* sig;                              // array of type T
       T** sig2D;
       Signal(void);                    // constructor
       Signal (int);                        // constructor
       Signal (int,int);
       Signal (int,double);                 // constructor      
       Signal(int,int,double);
       Signal (int,double,double);          // constructor
       Signal(int,int,double,double);
};


template <class T> class Img:public Signal<T>
{
public:
    Img(void);
    ~Img(void);
    Img(int,int);
};


template <class T> Img<T>::Img(int a,int b){
    Signal(a,b);  // or Signal<T>::Signal(a,b);
}

int main() {

    Img<int> *a=new Img<int>(2,3);
}

【问题讨论】:

    标签: c++ class templates inheritance linker


    【解决方案1】:

    你需要在初始化列表中初始化基类:

    template <class T> Img<T>::Img(int a,int b) :
        Signal<T>(a,b)  // here
    {
        // not here
    }
    

    您的版本尝试默认构造基础对象,因为它没有在初始化列表中提及,然后创建和销毁一个临时本地对象。

    【讨论】:

    • @user3094708:如果您还有其他问题,那么您应该再问一个问题——很难阅读 cmets 中的代码,或者提供足够的细节来清楚地描述问题。如果getHeight 是公开的,那么它应该是可访问的。
    • @user3094708:如果您还有其他问题,那么您应该提出其他问题 - 作为问题,而不是对此答案的评论。
    • 我意识到即使是公共的我也无法访问,但在派生类中声明之前我应该​​使用 Signal::getHeight 并且它可以工作。
    猜你喜欢
    • 2016-07-21
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2016-03-21
    • 2023-03-28
    • 2015-05-28
    • 2012-10-31
    相关资源
    最近更新 更多