Having trouble with, um, Class setup?

This is a discussion on Having trouble with, um, Class setup? within the C++ Programming forums, part of the General Programming Boards category; Okay, So the Manager template class needs to push_back a type resource_t.. that would be fine and dandy if I ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Having trouble with, um, Class setup?

    Okay, So the Manager template class needs to push_back a type resource_t.. that would be fine and dandy if I could get the Resource_Manager class with the Manager template in it to have the right type! Let me post code to explain my point further..

    Code:
    #pragma warning(disable: 4786)
    
    #include <vector>
    #include "MS3D.H"
    #include <map>
    
    class Resource_Cache // The "containers" and their data structures
    {
    friend class Resource_Manager;
    public:
    	struct	MS3DModelRenderData
    	{
    		float Position[3]; // will be changed to vectors eventually
    		float Orientation[3];
    		MS3DModel *pModel;
    	};
    
    	std::vector<MS3DModelRenderData*> MS3DModels; // holds pointers to all MS3D Models
    private:
    
    	bool Add_MS3DModel_To_Cache(std::string & Filename); // loads a milkshape model to the cache
    
    };
    
    template < typename resource_t >
    class Manager					// container managers
    {
    	Resource_Cache Global_Cache;
        std::map< std::string , resource_t* > Resources;
    
    public:
    
        bool Check_Existance( const std::string & filename );
    
    	void Return_Resource_Reference( const std::string & filename )
    	{
    		std::map< std::string , resource_t* >::iterator entry = Resources.find( filename );
    		if ( entry != Resources.end() ) // if the entry is found
    		{ 
    			Global_Cache.MS3DModels.push_back(entry->second);
    		} 
    	}
    
    	virtual ~Manager( void )
    	{
    		std::map< std::string ,  resource_t * >::iterator destroyer, end;
    		for ( destroyer = Resources.begin() , end = Resources.end() ; destroyer != end ; ++destroyer ) 
    		{
    			delete destroyer->second;
    		}
    	}	
    
    };
    
    class Resource_Manager	// Global Manager
    {
        Manager<MS3DModel> Models;  <---- needs to be of type MS3DModelRenderData   
    	std::string get_extension(std::string Filename);
    
    public:
    
    	void Add_Resource(std::string & Filename)
    	{
    		if (get_extension (Filename ) == "ms3d")
    		{
    			if (Models.Check_Existance( Filename ) == true )
    			{
    				Models.Return_Resource_Reference( Filename );
    			}
    			else
    			{
    			//	GlobalResources.Add_MS3DModel_To_Cache(Filename);
    			}
    		}
    		
    	}
    
    };
    The problem with the bolded statement in that code is, well, the manager template class uses an Instance of Resource_Cache, which is GlobalCache...

    Now I just can't declare another Global_Cache in Resource_Manager, I'd be trying to work with two different instances of the same thing, that's no good..

    How do I keep it so I'm still working with Manager's GlobalCache but make it so I can template Global_Cache.MS3DModelRenderData...

    Do I need to do like.. Manager::Global_Cache.MS3DModelRenderData ???

    Here is the error btw..

    Code:
    --------------------Configuration: Resource_Manager - Win32 Debug--------------------
    Compiling...
    Resource_Manager.cpp
    c:\documents and settings\jonathan\my documents\c++ programming\resource_manager\resource_manager.h(40) : error C2664: 'push_back' : cannot convert parameter 1 from 'class MS3DModel *' to 'struct Resource_Cache::MS3DModelRenderData *const & '
            Reason: cannot convert from 'class MS3DModel *' to 'struct Resource_Cache::MS3DModelRenderData *const '
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
            c:\documents and settings\jonathan\my documents\c++ programming\resource_manager\resource_manager.h(36) : while compiling class-template member function 'void __thiscall Manager<class MS3DModel>::Return_Resource_Reference(const class std::ba
    sic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)'
    Test.cpp
    c:\documents and settings\jonathan\my documents\c++ programming\resource_manager\resource_manager.h(40) : error C2664: 'push_back' : cannot convert parameter 1 from 'class MS3DModel *' to 'struct Resource_Cache::MS3DModelRenderData *const & '
            Reason: cannot convert from 'class MS3DModel *' to 'struct Resource_Cache::MS3DModelRenderData *const '
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
            c:\documents and settings\jonathan\my documents\c++ programming\resource_manager\resource_manager.h(36) : while compiling class-template member function 'void __thiscall Manager<class MS3DModel>::Return_Resource_Reference(const class std::ba
    sic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)'
    Error executing cl.exe.
    Creating browse info file...
    
    Resource_Manager.exe - 2 error(s), 0 warning(s)
    Hellpp please..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Nevermind, there was logic errors in what I was doing too...

    Shouldn't template a class with a specific vector to work with.. lol..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Fixed it, here is the updated code

    Code:
    #pragma warning(disable: 4786)
    
    #include <vector>
    #include "MS3D.H"
    #include <map>
    
    class Resource_Cache // The "containers" and their data structures
    {
    friend class Resource_Manager;
    public:
    	struct	MS3DModelRenderData
    	{
    		float Position[3]; // will be changed to vectors eventually
    		float Orientation[3];
    		MS3DModel *pModel;
    	};
    
    	std::vector<MS3DModelRenderData*> MS3DModels; // holds pointers to all MS3D Models
    private:
    
    	bool Add_MS3DModel_To_Cache(std::string & Filename); // loads a milkshape model to the cache
    
    };
    
    template < typename resource_t >
    class Manager					// container managers
    {
    Didn't need a Resource_Cache instance in here
        std::map< std::string , resource_t* > Resources;
    
    public:
    
        bool Check_Existance( const std::string & filename );
    
    	resource_t & Return_Resource_Reference( const std::string & filename )
    	{
    		std::map< std::string , resource_t* >::iterator entry = Resources.find( filename );
    		if ( entry != Resources.end() ) // if the entry is found
    		{ 
    			return * entry->second; <--  Was templating a non generic action before
    		} 
    	}
    
    	virtual ~Manager( void )
    	{
    		std::map< std::string ,  resource_t * >::iterator destroyer, end;
    		for ( destroyer = Resources.begin() , end = Resources.end() ; destroyer != end ; ++destroyer ) 
    		{
    			delete destroyer->second;
    		}
    	}	
    
    };
    
    class Resource_Manager	// Global Manager
    {
    
        Manager<Resource_Cache::MS3DModelRenderData> Models; <-- finally got what I wanted, no errors with this line, still don't need an instance of Resource_Cache to work with..
    	std::string get_extension(std::string Filename);
    
    public:
    
    	void Add_Resource(std::string & Filename)
    	{
    		if (get_extension (Filename ) == "ms3d")
    		{
    			if (Models.Check_Existance( Filename ) == true )
    			{
    				Models.Return_Resource_Reference( Filename );
    			}
    			else
    			{
    			//	GlobalResources.Add_MS3DModel_To_Cache(Filename);
    			}
    		}
    		
    	}
    
    };
    Basically I was doing something really silly that I wasn't supposed to be doing at all.. First of all, I was templating a push_back function using a specific vector not created in the template, BAD MOVE, would have caused aweful failed overload errors..

    Finally, here is the final function, no else yet, the else will be a whole new story, and require the actual filling out of the MS3DModelRenderData structure and then pushing that back..

    Code:
    	void Add_Resource(std::string & Filename)
    	{
    		if (get_extension (Filename ) == "ms3d")
    		{
    			if (Models.Check_Existance( Filename ) == true )
    			{
    				Global_Cache.MS3DModels.push_back(&Models.Return_Resource_Reference( Filename ));
    			}
    			else
    			{
    			//	GlobalResources.Add_MS3DModel_To_Cache(Filename);
    			}
    		}
    	}
    Last edited by Shamino; 01-24-2006 at 10:11 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 11:16 AM
  2. Having trouble with this class...
    By Qui in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2004, 04:30 PM
  3. Trouble with a class variable
    By TravisDane in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2002, 12:59 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21