繁体   English   中英

未定义对模板成员函数的引用

[英]Undefined reference to template member function

我正在使用Qt 5.5.1,并尝试为我的应用程序中的某些属性使用模板类,但是在构建时,我得到对该模板类的每种类型的未定义引用。

该版本曾经在MSVC ++ 2015中工作,但是切换到Qt之后,我相信我可能不会遵循某些语法约定吗?

这是我的头文件:

#include <array>
#include <string>
using namespace std;

template <const int SIZE>
class Property
{
public:
    Property(const array<pair<int, string>, SIZE>* properties);
    ~Property();
    string getPropertyValue(int code);
private:
    const array<pair<int, string>, SIZE>* mProperties;
};

这是我的源文件:

#include "Property.h"

template <const int SIZE>
Property<SIZE>::Property(const array<pair<int, string>, SIZE>* properties)
{
    mProperties = properties;
}

template <const int SIZE>
Property<SIZE>::~Property() {}

template <const int SIZE>
string Property<SIZE>::getPropertyValue(int code)
{
    for (int i = 0; i < SIZE; i++) 
    {
        if( code == mProperties[0][i].first )
        {
             return mProperties[0][i].second;
        }
    }

    string("no value found");
}

这是我的实现:

#include <iostream>
#include "Property.h"
using namespace std;

const int arrSize = 1;
const array<pair<int, string>, arrSize> arrValues{
    make_pair(0x02, string("String Value"))
};

int main()
{
    Property<arrSize>* properties = new Property<arrSize>(&arrValues);
    cout << properties->getPropertyValue(2) << endl;
    return 0;
}

这是我的构建输出:

undefined reference to `Property<1>::Property(std::array<std::pair<int, std::string>, 1u> const*)'
undefined reference to `Property<1>::getPropertyValue(int)'

我想拥有多个属性,并且编译器抱怨每个不同的大小Property <2> :: ... ... Property <44> :: ...等...任何帮助将不胜感激。 另外,这样进行代码/值查找的更好方法是什么?

谢谢!

模板仅需要在头文件中定义,这就是为什么要获取未定义引用的原因。 将源模板代码文件移至头文件后,我便能够成功运行此文件。

暂无
暂无

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

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