Thread: Templated member functions

  1. #1
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44

    Templated member functions

    Hi, I'm trying to create a settings registry for a project I'm working on, and I've come into a problem. First, my code:

    CSettingManager header
    Code:
    #ifndef __CSETTINGMANAGER
    #define __CSETTINGMANAGER
    
    // Include headers
    #include <sstream>
    #include <map>
    #include <string>
    #include "CSingleton.h"
    
    // Setting manager class
    class CSettingManager : public CSingleton<CSettingManager>
    {
    
    	public:
    		CSettingManager();
    		~CSettingManager();
    		template <class T> bool addSetting(std::string name, T value);
    		template <class T> bool editSetting(std::string name, T value);
    		template <class T> bool getSetting(std::string name, T* outVar);
    		bool isSetting(std::string name);
    		bool loadSetting(std::string name);
    		bool saveSettings(std::string name);
    		void resetSettings();
    	private:
    		std::map<std::string, std::stringstream> Registry;
    };
    
    #endif	//__CSETTINGMANAGER
    As you can see, I have some templated functions that I can use to add any type of data to my setting registry. Here's the implementation of these functions.

    Code:
    // Add setting
    // Adds a setting to the setting registry
    template <class T>
    bool CSettingManager::addSetting(std::string name, T value)
    {
    	// Check to see if the setting is already in the registry
    	if (Registry.find(name) == Registry.end())
    	{
    		// Create a stringstream with the value in it and add it to the registry
    		std::stringstream RegValue;
    		RegValue << value;
    		Registry[name] = RegValue;
    		return true;
    	}
    	return false;
    }
    
    // Edit Setting
    // Changes a value in the settings registry
    template <class T>
    bool CSettingManager::editSetting(std::string name, T value)
    {
    	// Check to see if the setting is in the registry
    	if (Registry.find(name) != Registry.end())
    	{
    		std::stringstream RegValue;
    		RegValue << value;
    		Registry[name] = RegValue;
    		return true;
    	}
    	return false;
    }
    
    // Get Setting
    // Returns a setting in the registry
    template <class T>
    bool CSettingManager::getSetting(std::string name, T* outVar)
    {
    	// Check to see if the setting is in the registry
    	if (Registry.find(name) == Registry.end())
    	{
    		return false;
    	}
    
    	// Convert the stringstream in the registry to the native data type
    	Registry[name] >> *outVar;
    	return true;
    }
    I don't get any other errors other than the following when I try to add a setting.
    Code:
    undefined reference to `bool CSettingManager::addSetting<int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
    My main function looks like this:
    Code:
    int main()
    {
    	CSettingManager* Manager = CSettingManager::getInstancePtr();
    	Manager->addSetting(string("Test"), 16);
    	Manager->drop();
    }
    Is what I'm doing possible, having templated member function in a non-templated class? If anyone can help me out, I'd really appreciate it.
    Last edited by Highland Laddie; 05-15-2006 at 04:25 PM.
    Bagpipes – putting the fun back in funeral.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    First, isn't that code from Gamedev.net ? ;-)
    Second, templated functions have to be defined in header files, not implementation files.

  3. #3
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Yeah, a lot of the code I'm using is based on the enginuity tutorials at gamedev, but I'm just trying to use the ideas from those articles, not the actual code.

    I did define those functions in a header first, then I implemented them in a .cpp file, or did you mean something different?
    Bagpipes – putting the fun back in funeral.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Templates usually need to be both declared and defined in the same file(usually header).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Well, I've boiled my problem down to a more simple one. I can't make a std::map with a stringstream as I want to, it won't compile. I'm thinking of making a setting class with methods to get, set, and edit the name and values, and just store that in the setting manager as a vector. I'll post whether it works or not.
    Bagpipes – putting the fun back in funeral.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I can't make a std::map with a stringstream as I want to, it won't compile.
    What's the error? It compiles ok for me.

    BTW, another option is Boost::any or Boost::variant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 06:59 AM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. How do you call member functions
    By Elite in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2003, 01:03 PM