Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Template class syntax error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 08 November 2012 - 10:03 PM

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 ';'
An invisible text.

Sponsor:

#2 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 08 November 2012 - 10:27 PM

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, 08 November 2012 - 10:52 PM.

An invisible text.

#3 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 09 November 2012 - 03:19 AM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS