I have the following code:
struct All {
All() {}
~All() {}
template <typename T>
static struct Item {
T var;
} item;
virtual void setVal() noexcept {}
};
template <typename T>
struct any : public All
{
public:
any() : All() {}
~any() {}
T value;
void setVal() noexcept override {
All::item<decltype(value)>.var = value; // Error appears here
}
};
And the following error:
undefined reference to
All:item<int>
I don't understand this error, because item is a static member variable template, and i have to specialize it...
Help me !
Your bug is that you never defined the static member variable. You only declared it. Add a definition:
template <typename T>
All::Item<T> All::item = {};
However, your syntax for defining the type of the member variable template and declaring the variable itself is not accepted by the compilers that I tested. g++ (5.2.0) is fine with the declaration, but complains that All::Item
is not a template when specifying the type for the variable definition. clang++ (3.6.0) does not even accept your declaration. Simply separating the definition of the type and the declaration of the variable solved the issue:
template <typename T>
struct Item {
T var;
};
template <typename T>
static Item<T> item;