• Advertisement
Sign in to follow this  

Template class syntax error

This topic is 1896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some trouble writing Resource class and ResourceManager template class

ResourceManager.h
[source lang="cpp"]#pragma once
#include <unordered_map>
#include <utility> //for std::pair
#include "Resource.h"
#include <string>

template<class T> class ResourceManager{friend class Resource<T>;};

template<>
class ResourceManager<sf::Music>
{
private:
std::unordered_map<std::string, std::pair<int, sf::Music>> resourceMap;
public:
};[/source]

Resource.h
[source lang="cpp"]#pragma once
#include <SFML\Audio.hpp>
#include "ResourceManager.h"
template<class T> class Resource;


template<>
class Resource<sf::Music>
{
private:
static ResourceManager<sf::Music> manager;//Cause of error
};[/source]

Errors I get;
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'

Share this post


Link to post
Share on other sites
Advertisement
I fixed the error and now must define a constructor

ResourceManager.h
[source lang="cpp"]#pragma once
#include <unordered_map>
#include <utility> //for std::pair
#include <SFML/Audio.hpp>
#include <string>

template<class T> class Resource;

template<class T> class ResourceManager{friend class Resource<T>;};

template<>
class ResourceManager<sf::Music>
{
private:
std::unordered_map<std::string, std::pair<int, sf::Music>> resourceMap;
public:
ResourceManager();
};

ResourceManager<sf::Music>::ResourceManager()
{
}[/source]

Resource.h
[source lang="cpp"]#pragma once
#include <SFML\Audio.hpp>
#include "ResourceManager.h"


template<>
class Resource<sf::Music>
{
private:
static ResourceManager<sf::Music> manager;
};
[/source]

When I compile this, I get linker error
1>HomeMenuScene.obj : error LNK2005: "public: __thiscall ResourceManager<class sf::Music>::ResourceManager<class sf::Music>(void)" (??0?$ResourceManager@VMusic@sf@@@@QAE@XZ) already defined in LogicManager.obj
1>main.obj : error LNK2005: "public: __thiscall ResourceManager<class sf::Music>::ResourceManager<class sf::Music>(void)" (??0?$ResourceManager@VMusic@sf@@@@QAE@XZ) already defined in LogicManager.obj
1>PlayingScene.obj : error LNK2005: "public: __thiscall ResourceManager<class sf::Music>::ResourceManager<class sf::Music>(void)" (??0?$ResourceManager@VMusic@sf@@@@QAE@XZ) already defined in LogicManager.obj
1>SettingMenuScene.obj : error LNK2005: "public: __thiscall ResourceManager<class sf::Music>::ResourceManager<class sf::Music>(void)" (??0?$ResourceManager@VMusic@sf@@@@QAE@XZ) already defined in LogicManager.obj
1>C:\Users\Park\Documents\Visual Studio 2012\Projects\Game3\Debug\Game3.exe : fatal error LNK1169: one or more multiply defined symbols found Edited by lride

Share this post


Link to post
Share on other sites
Put the constructor to .cpp or inline it.

ResourceManager<sf::Music>::ResourceManager()
{
}

It should be in .cpp file as you specialized the template, or just add inline before the declaration.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement