-
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 :)
-
Not sure about this...
but shouldn't it be instead:
ia >> Data.get()
or are you overloading >>?
-
<< 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.
-
Try using header inclusion guards, since that function is not templated, it's implementation should really be in the source(.cpp) file.
-
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
-
>> 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?
-
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
}
-
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.
-
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?
-
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>.
-
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
-
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.
-
Sounds like exactly what I want :)
-
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.
-
I don't know what that means. Can you explain it in more detail?