Syntax issues..

This is a discussion on Syntax issues.. within the C++ Programming forums, part of the General Programming Boards category; Okay, so in my load file function, I'm loading an object manager from file, I need to create a shared ...

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

    Syntax issues..

    Okay, so in my load file function, I'm loading an object manager from file, I need to create a shared pointer to the object manager, which I'm assuming are all the same size, I mean the pointer. The actual managers might be a different size, but I figure as long as I'm just storing a pointer to an object manager it will be the same size..

    Then I need to put them in a map, paired with the filename.... Here's the code:
    Code:
    class Database
    {
    public:	
    
    	typedef boost::shared_ptr<Object_Manager> Raw_Manager;
    	typedef boost::weak_ptr<Object_Manager> Data_Observer;
    	typedef std::map<std::string, Raw_Manager> Manager_Map;
    	
    	// Function to access a specific manager within the map
    	Data_Observer Access_Manager(std::string Filename)
    	{
    		Manager_Map::iterator it = Database_Manager.find(Filename);
    		// check if the manager exists in the map
    		if (it == Database_Manager.end())
    		{
    			// if not throw an error
    			std::cout << "Manager does not exist!" << std::endl;
    		}
    		else
    		{
    			// if so return a pointer to the manager
    			return Data_Observer(it->second);
    		}
    	}
    
    	//Save a manager completely to a file
    	template <typename T>
    	void Save_to_File(std::string Filename, Object_Manager Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
    		Data.erase();
        	// archive and stream closed when destructors are called
    	}
    
    	// Load a manager from a file and add it to the list of managers
    	void Read_File(std::string Filename, Raw_Manager Data)
    	{
            // create and open an archive for input
            std::ifstream ifs(Filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> Data;
    		// create a pointer to the data
    		Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Data));
            // archive and stream closed when destructors are called
    	}
    private:
    	Manager_Map Database_Manager;
    };
    Here's the error I'm getting:
    Code:
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(57) : error C2082: redefinition of formal parameter 'Data'
    Thanks for the help
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    Not sure about this...

    but shouldn't it be instead:

    ia >> Data.get()

    or are you overloading >>?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    << is output to a file, and >> is input, its in the Archive class that boost uses....

    You gotta << output Data, and you gotta copy the data to a new Data object when you >> input it. Then I need to create a pointer to the copy of the file, and put that in the map.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Try using header inclusion guards, since that function is not templated, it's implementation should really be in the source(.cpp) file.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I had this working at one point then I changed my code, now I'm trying to change back, and it's not going as smoothly... I have header inclusion guards on, you mean these?

    Code:
    #ifndef DATABASE_H
    #define DATABASE_H
    
    
    #endif
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I have header inclusion guards on, you mean these?
    Yes.

    Which is line 57? You might as well post the whole header file if you are going to post most of it. You might be missing a #include that includes the declaration of shared_ptr or ObjectManager or something like that.

    Is that one line your only error message?

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    This it the whole header, it's no big secret, but I'm purty sure I'm not missing anything:

    Code:
    #ifndef DATABASE_H
    #define DATABASE_H
    
    #include "Object.h"
    #include <map>
    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <string>
    #include <vector>
    
    class Database
    {
    public:	
    
    	typedef boost::shared_ptr<Object_Manager> Raw_Manager;
    	typedef boost::weak_ptr<Object_Manager> Data_Observer;
    	typedef std::map<std::string, Raw_Manager> Manager_Map;
    	
    	// Function to access a specific manager within the map
    	Data_Observer Access_Manager(std::string Filename)
    	{
    		Manager_Map::iterator it = Database_Manager.find(Filename);
    		// check if the manager exists in the map
    		if (it == Database_Manager.end())
    		{
    			// if not throw an error
    			std::cout << "Manager does not exist!" << std::endl;
    		}
    		else
    		{
    			// if so return a pointer to the manager
    			return Data_Observer(it->second);
    		}
    	}
    
    	//Save a manager completely to a file
    	template <typename T>
    	void Save_to_File(std::string Filename, Object_Manager Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
    		Data.erase();
        	// archive and stream closed when destructors are called
    	}
    
    	// Load a manager from a file and add it to the list of managers
    	void Read_File(std::string Filename, Object_Manager Data)
    	{
            // create and open an archive for input
            std::ifstream ifs(Filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> Data;
    		// create a pointer to the data
    		new Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Data));
            // archive and stream closed when destructors are called
    	}
    private:
    	Manager_Map Database_Manager;
    };
    
    #endif
    Here is my current error message:
    Code:
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(57) : error C2664: 'boost::shared_ptr<T>::shared_ptr(const boost::shared_ptr<T> &)' : cannot convert parameter 1 from 'Object_Manager' to 'const boost::shared_ptr<T> &'
    It points to this line:
    Code:
    	void Read_File(std::string Filename, Object_Manager Data)
    	{
            // create and open an archive for input
            std::ifstream ifs(Filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> Data;
    		// create a pointer to the data
    		new Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Data));
            // archive and stream closed when destructors are called
    	}
    Last edited by Shamino; 07-03-2007 at 12:03 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I believe what they want to do is convert Data from an Object_Manager to a Raw_Manager, so it can be inserted into the Database_Manager map. But I can't see how that line would accomplish this.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Doesn't this:
    Code:
    new Raw_Manager(Data);
    Create a raw manager of type Data?

    It's obviously not working, but I've done something similar to this before, I'm just goofing up some how, I must be using my variables incorrectly somehow.... I forget what exactly to do though...

    Any help on this?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Maybe:
    Code:
    Raw_Manager rm(new Object_Manager(Data));
    That creates a shared_ptr initialized with the pointer to newmemoery that holds a new Object_Manager that was copy constructed with Data. Is that what you wanted?

    BTW, it looks like you're missing <fstream>.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Hey sweet, thats the exact tweak I needed to make it work, thanks, can you explain maybe the theory behind the change, so I don't make the mistake again and all :d

    And as far as the fstream thing goes, I'm using boost archives, not the fstream. Unless maybe I'm mistaken, which is always possible :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Raw_Manager is a typedef for a shared_ptr. A shared_ptr is constructed with a regular plain pointer. Usually you call new to create a new object and pass the result to the shared_ptr constructor.

    So here, you are constructing a new shared_ptr named rm and passing the raw pointer from new to its constructor.

    The shared_ptr holds a pointer to an Object_Manager. So you have to create a new Object_Manager to store in there. That's where the new Object_Manager part of the code comes in.

    Finally, you wanted your new Object_Manager to be a copy (I hope) of the Object_Manager parameter called Data. So to create a new object as a copy of an existing one, just pass that existing one to its constructor.

    So new Object_Manager(Data) creates a new Object_Manager that is a copy of Data. The pointer returned by new is saved in the shared_ptr object called rm.

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Sounds like exactly what I want
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Only the pointer I'm creating for the object manager is meant to be a copy of what is in the file that I load using boost.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't know what that means. Can you explain it in more detail?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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