簡體   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