sorry, if this gets a little complicated. I’ve taken the object and removed everything superfluous to narrow down the cause of the error. This is what’s causing my error:
d3d11module.h
#ifndef STUFF_H
#define STUFF_H
#include <map>
#include <string>
#include <memory>
class D3D11
{
public:
class Shader;
template<class T>
class ShaderRegister;
typedef std::map<std::string, std::shared_ptr<Shader>(*)()> SHADER_CREATE_MAP;
private:
static SHADER_CREATE_MAP createMap;
};
class D3D11::Shader
{
};
template<class T>
std::shared_ptr<D3D11::Shader> createShader() {return std::shared_ptr<D3D11::Shader>(new T);}
template<class T>
class D3D11::ShaderRegister
{
public:
ShaderRegister(std::string const & s)
{
D3D11::createMap.insert(std::pair<std::string,std::shared_ptr<Shader>(*)()>(s, &createShader<T>));
}
};
#endif
just a real quick explanation of all this. There is a class called d3d11 that contains a couple of classes. First the shader class which classes are to be able to derive and implement, but is left blank here because it’s contents have nothing to do with the problem. then i have a map of string indices, that correspond to function pointers.
the ShaderRegister object is to be a static private member within the derived shader classes. Then just outside, the constructor is invoked so that a specified index and a pointer to createShader() is stored at that index. By doing this, invoking the index will call this templated function, returning an instance of that derived shader.
I get the linking error though, when trying to derive the shader object and createing an instance of ShaderRegister and trying to call it’s constructor. Take a look:
#ifndef DIFFUSE_SHADER
#define DIFFUSE_SHADER
#include "d3d11module.h"
class DiffuseShader : public D3D11::Shader
{
static D3D11::ShaderRegister<DiffuseShader> reg;
};
D3D11::ShaderRegister<DiffuseShader> DiffuseShader::reg("DiffuseShader");
#endif
doing that causes a quite a painful looking link error.
main.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::tr1::shared_ptr<class D3D11::Shader> (__cdecl*)(void),struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::tr1::shared_ptr<class D3D11::Shader> (__cdecl*)(void)> > > D3D11::createMap" (?createMap@D3D11@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@P6A?AV?$shared_ptr@VShader@D3D11@@@tr1@2@XZU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@P6A?AV?$shared_ptr@VShader@D3D11@@@tr1@2@XZ@std@@@2@@std@@A)
still, what i think it’s trying to say is the static map object in the d3d11 class is undefined when used at some point. But i thought static objects were constructed at the beginning of the program. So what’s going on?
Static members are declared in the class like everything else, but they also must be defined somewhere in one and only one source (.cpp) file. It’s the same as declaring a member function prototype, then defining the body of the function somewhere else.