I know that this is probably a stupid question :\ but I am stupid...
I am getting 1>angledCollision.obj : error LNK2001: unresolved external symbol "protected: static class angledCollision * Singleton<class angledCollision>::ms_Singleton" (?ms_Singleton@?$Singleton@VangledCollision@@@@1PAVangledCollision@@A) 1>spawningArea.obj : error LNK2001: unresolved external symbol "protected: static class angledCollision * Singleton<class angledCollision>::ms_Singleton" (?ms_Singleton@?$Singleton@VangledCollision@@@@1PAVangledCollision@@A) I know the problem is that a source file wasn't found, but I can't see where...

Singleton hedder(theres no .cpp implementation):

#ifdef _MSVC
  // Turn off warnings generated by this singleton implementation
  #pragma warning (disable : 4311)
  #pragma warning (disable : 4312)
#endif
#ifndef SINGLETON_H
#define SINGLETON_H

#include <assert.h>

  template <typename T> class Singleton
  {
  protected:

    static T* ms_Singleton;

  public:

    Singleton()
    {
      assert( !ms_Singleton );
      int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
      ms_Singleton = (T*)((int)this + offset);
    }
    ~Singleton(){ assert( ms_Singleton ); ms_Singleton = 0; }
      
    static T* getSingleton() { return ms_Singleton; }

  };
#endif

here is where I declared that angledCollision is a child of singleton

class angledCollision : public Singleton<angledCollision>

and finally, here are my #include's for both linking error files:
angledCollision:

#include <vector>
#include <math.h>
#include <glut.h>
#include "aCNode.h"
#include "Singleton.h"

spawningArea:

#include <math.h>
#include <iostream>
#include "render.h"
#include "loadTex.h"
#include <gl/glut.h>
#include "TextureLoader.h"
#include "Vector3D.h"
...
class spawningArea : public render // thus ill also show you render's #include's

render:

#include "texturedPolygons.h"
#include "angledCollision.h"

I did try #include'ing "angledCollision.h" inside of spawningArea, it changed nothing

so why am I still getting linking errors? if you need any more source just ask, decided not to dump everything because I don't think the internet is big enough :P


btw, If you see my history, I haven't answered any questions, Ive been trying to answer un-solved questions, but my solutions are always overly complex and may be hard to understand, so I don't bother, so I am trying to help the comunity, not just leach.


###EDIT###
oh, btw, before I make anything using angledCollision, i have spcified:

//initiates angledCollision singleton
	new angledCollision;

Dani AI

Generated

Brief summary: the linker error means the static member ms_Singleton was declared but no storage was provided for it. A static data member inside a class (including a class template) needs one definition at namespace scope so the linker can find it — the declaration inside the template does not allocate storage by itself.

Concrete fix (minimal change)
Create one .cpp in your project (for example SingletonInstantiations.cpp) and put a definition for the angledCollision specialization there:

#include "angledCollision.h"
#include "Singleton.h"

// define storage for the angledCollision specialization
template<> angledCollision* Singleton<angledCollision>::ms_Singleton = 0;

Build that .cpp into the project. That gives the linker the single definition it needs. On modern compilers you can use nullptr instead of 0, but use what your compiler supports.

Safer alternatives

  • Prefer the Meyers singleton (function-local static) to avoid pointer-management and ODR problems:
class angledCollision {
public:
  static angledCollision& instance() {
    static angledCollision s;
    return s;
  }
  angledCollision(const angledCollision&) = delete;
  angledCollision& operator=(const angledCollision&) = delete;
private:
  angledCollision() = default;
  ~angledCollision() = default;
};

This avoids needing an out-of-class static definition and is thread-safe since C++11.

Extra notes and cautions

  • Defining template<class T> T* Singleton<T>::ms_Singleton = 0; in a header can create multiple-definition issues on some compilers; prefer the explicit specialization in one TU.
  • The pointer-offset trick in the constructor is fragile (breaks on 64-bit, with multiple inheritance, or strict aliasing). Replace it with a clear, safe initialization approach when possible.
  • was on the right track about templates being instantiated on demand; ’s workaround of making angledCollision its own singleton works, but the explicit specialization above is the direct solution to the linker error.

Recommended Answers

All 2 Replies

I know that this is probably a stupid question :\ but I am stupid...
I am getting 1>angledCollision.obj : error LNK2001: unresolved external symbol "protected: static class angledCollision * Singleton<class angledCollision>::ms_Singleton" (?ms_Singleton@?$Singleton@VangledCollision@@@@1PAVangledCollision@@A) 1>spawningArea.obj : error LNK2001: unresolved external symbol "protected: static class angledCollision * Singleton<class angledCollision>::ms_Singleton" (?ms_Singleton@?$Singleton@VangledCollision@@@@1PAVangledCollision@@A) I know the problem is that a source file wasn't found, but I can't see where...

Singleton hedder(theres no .cpp implementation):

#ifdef _MSVC
  // Turn off warnings generated by this singleton implementation
  #pragma warning (disable : 4311)
  #pragma warning (disable : 4312)
#endif
#ifndef SINGLETON_H
#define SINGLETON_H

#include <assert.h>

  template <typename T> class Singleton
  {
  protected:

    static T* ms_Singleton;

  public:

    Singleton()
    {
      assert( !ms_Singleton );
      int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
      ms_Singleton = (T*)((int)this + offset);
    }
    ~Singleton(){ assert( ms_Singleton ); ms_Singleton = 0; }
      
    static T* getSingleton() { return ms_Singleton; }

  };
#endif

here is where I declared that angledCollision is a child of singleton

class angledCollision : public Singleton<angledCollision>

and finally, here are my #include's for both linking error files:
angledCollision:

#include <vector>
#include <math.h>
#include <glut.h>
#include "aCNode.h"
#include "Singleton.h"

spawningArea:

#include <math.h>
#include <iostream>
#include "render.h"
#include "loadTex.h"
#include <gl/glut.h>
#include "TextureLoader.h"
#include "Vector3D.h"
...
class spawningArea : public render // thus ill also show you render's #include's

render:

#include "texturedPolygons.h"
#include "angledCollision.h"

I did try #include'ing "angledCollision.h" inside of spawningArea, it changed nothing

so why am I still getting linking errors? if you need any more source just ask, decided not to dump everything because I don't think the internet is big enough :P


btw, If you see my history, I haven't answered any questions, Ive been trying to answer un-solved questions, but my solutions are always overly complex and may be hard to understand, so I don't bother, so I am trying to help the comunity, not just leach.


###EDIT###
oh, btw, before I make anything using angledCollision, i have spcified:

//initiates angledCollision singleton
	new angledCollision;

Hey, this is not my area of expertise. Does this quote from cplusplus.com help any?

Templates and multiple-file projects
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a "header file" with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.

I decided that the strain it was causing me + the time restraints I had, I couldn't follow that path, so I made the class angledCollision a singleton, rather than making it's parent a singleton, it's working now, thanks anyway :) if I ever try the singleton again Ill keep you'r advice in mind, thank you very much

:| sorry I couldn't stick around long enough to see if I could fix problem so others could

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.