C++ 对模板函数的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10632251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:17:14  来源:igfitidea点击:

undefined reference to template function

c++qtboostundefined-reference

提问by Vihaan Verma

I have three files . The contents of main.cpp are

我有三个文件。main.cpp 的内容是

#include<iostream>
#include<QString>

#include "util.h"

int main()
{
    using Util::convert2QString;

    using namespace std;
    int n =22;
    QString tmp = convert2QString<int>(n);

    return 0;
}

util.h

实用程序

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);
}

util.cpp

实用程序

namespace Util
{
    template<class T>
        QString convert2QString(T type, int digits=0)
        {
            using std::string;

            string temp = (boost::format("%1%") % type).str();

            return QString::fromStdString(temp);
        }
}

When I try to compile these files with following command I get undefined reference error

当我尝试使用以下命令编译这些文件时,出现未定义的引用错误

vickey@tb:~/work/trash/template$ g++ main.cpp  util.cpp -lQtGui -lQtCore  -I. -I/usr/local/Trolltech/Qt-4.8.0/include/QtCore -I/usr/local/Trolltech/Qt-4.8.0/include/QtGui -I/usr/local/Trolltech/Qt-4.8.0/include
/tmp/cca9oU6Q.o: In function `main':
main.cpp:(.text+0x22): undefined reference to `QString Util::convert2QString<int>(int, int)'
collect2: ld returned 1 exit status

Is there something wrong with the template declaration or implementation ? why M I getting these linking errors :?

模板声明或实现有问题吗?为什么 MI 收到这些链接错误:?

回答by Luchian Grigore

The implementation of a non-specialized template must be visible to a translation unit that uses it.

非专用模板的实现必须对使用它的翻译单元可见。

The compiler must be able to see the implementation in order to generate code for all specializations in your code.

编译器必须能够看到实现,才能为代码中的所有特化生成代码。

This can be achieved in two ways:

这可以通过两种方式实现:

1) Move the implementation inside the header.

1) 将实现移到标题内。

2) If you want to keep it separate, move it into a different header which you include in your original header:

2)如果您想将其分开,请将其移动到您包含在原始标题中的不同标题中:

util.h

实用程序

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);
}
#include "util_impl.h"

util_impl.h

util_impl.h

namespace Util
{
    template<class T>
        QString convert2QString(T type, int digits=0)
        {
            using std::string;

            string temp = (boost::format("%1") % type).str();

            return QString::fromStdString(temp);
        }
}

回答by inkooboo

You have 2 ways:

你有两种方式:

  1. Implement convert2QStringin util.h.

  2. Manually instantiate convert2QStringwith intin util.cpp and define this specialization as extern function in util.h

  1. convert2QString在 util.h 中实现。

  2. 在 util.cpp 中手动实例化convert2QStringint在 util.h 中将此特化定义为 extern 函数

util.h

实用程序

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);

    extern template <> QString convert2QString<int>(int type , int digits);
}

util.cpp

实用程序

 namespace Util {
     template<class T>
     QString convert2QString(T type, int digits)
     {
         using std::string;

         string temp = (boost::format("%1") % type).str();

         return QString::fromStdString(temp);
     }

     template <> QString convert2QString<int>(int type , int digits); 
}