简体   繁体   English

C ++中的模板

[英]Templates in c++

I've some problem with using templates: 我在使用模板时遇到一些问题:

myclass.h myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class myclass
{

private:


public:

    ~myclass();
     myclass();
    template<class S>
    void login(S login, S pass);
};



#endif // MYCLASS_H

myclass.cpp myclass.cpp

#include "myclass.h"
#include "../additional_func.h" // for connect(QString,QString) function


myclass::myclass()
{

}

template <typename S>
void myclass::login(S login, S pass)
{

    additional_func->connect(login,pass);

}

myclass::~myclass()
{

}

in mainwindow.cpp (i'm using QT) 在mainwindow.cpp中(我正在使用QT)

   myclass *vr = new vr();
   vr->login(ui->linelogin->text(),ui->linepwd->text()); // QString, QString

And I get error: 我得到错误:

mainwindow.cpp:31: error: undefined reference to `void ecore::connect(QString, QString)' mainwindow.cpp:31:错误:对`void ecore :: connect(QString,QString)'的未定义引用

What way for using myclass::connect in other classes? 在其他类中使用myclass :: connect的方法是什么?

You've declared login() within myclass in your header, but you don't show us here that you've defined it anywhere. 您已经在标题的myclass中声明了login() ,但是这里没有向我们显示您已在任何地方定义了它。

Putting a semicolon after the function signature simply declares that this function exists. 在函数签名之后加上分号只是声明该函数存在。 It doesn't create a function. 它不会创建函数。 You have to define the function in order to use it. 您必须定义函数才能使用它。

You can define it within your header, by putting the function body in {} instead of a semicolon. 您可以在标头中定义它,方法是将函数体放在{}而不要使用分号。

class myclass{
    // ...

    template<class S>
    void login(S login, S pass){
        // code goes here
    }
}

Placing the definition here is one way to indicate to the compiler that you wish to inline the function, although the compiler may choose not to, anyway. 在此处放置定义是一种向编译器指示您希望内联函数的方法,尽管编译器可能会选择不这样做。

Or, you could define it within your .cpp file just like the other functions you've defined. 或者,您可以在.cpp文件中定义它,就像定义的其他函数一样。

 
 
 
  
  template<class S> void myclass::login(S login, S pass){ // code goes here }
 
  

Because this is a template function that will (presumably) be shared by multiple code files, it has to be defined within the header. 因为这是一个模板函数,(可能)将由多个代码文件共享,所以必须在标头中定义它。

Without one of these, there is no function body defined for the compiler to execute when you call this function, hence the "undefined reference" error. 如果没有其中之一,则在您调用此函数时,没有为编译器定义要执行的函数体,因此会出现“未定义的引用”错误。

You should put the implementation of your template function in the header file, not in a .cpp file. 您应该将模板功能的实现放在头文件中,而不要放在.cpp文件中。

A template's definition needs to be available to the compiler when it should be instantiated. 模板的定义应在实例化时可供编译器使用。 At the point where you call vr->login(...) the compiler needs to have the template's definition (the function body) available to create an instance login<QString>() . 在调用vr->login(...) ,编译器需要具有可用于创建实例login<QString>()的模板定义(函数主体login<QString>() If the template function's body is "hidden" in some .cpp file then no function is created, and later the linker complains that it is missing. 如果模板函数的主体在某些.cpp文件中“隐藏”,则不会创建任何函数,随后链接器会抱怨缺少该函数。

在.cpp文件中,该方法名为start(),而不是login()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM