Reputation: 324
I'm using Qt 5.5.1 and am trying to use a template class for some properties in my application but on build I'm getting undefined references to each type of that template class.
This build used to work in MSVC++ 2015 but after switching to Qt I believe I might not be following some syntactical convention?
Here is my header file:
#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;
};
Here is my source file:
#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");
}
Here is my implementation:
#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;
}
Here is my build output:
undefined reference to `Property<1>::Property(std::array<std::pair<int, std::string>, 1u> const*)'
undefined reference to `Property<1>::getPropertyValue(int)'
I want to have multiple properties and the compiler complains about each different size Property<2>:: ... Property<44>:: ... etc... Any help would be greatly appreciated. Also, what is a better way to do code/value lookup like this?
Thanks!
Upvotes: 1
Views: 1147
Reputation: 73
Templates need to only be defined within header files, this is why you are getting an undefined reference. I was able to run this successfully once I moved the source template code file into the header file.
Upvotes: 2