Thread: Syntax error in map instantiation.. probably something silly :)

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

    Syntax error in map instantiation.. probably something silly :)

    Here's the code with the errors...

    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:	
    
    	std::map<std::string name, std::vector<boost::shared_ptr<T>> Data;
    	typedef boost::weak_ptr<Data> Data_Observer;
    	// 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
    	void Save_to_File(std::string Filename, std::map<std::string name, std::vector<boost::shared_ptr<T_>>)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
    		Data.~Object_Manager();
        	// 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
    		Manager_Ptr Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Raw_Manager));
            // archive and stream closed when destructors are called
    	}
    private:
    	Manager_Map Database_Manager;
    };
    
    #endif
    And here are the errors... theres on "fatal" error that it can't recover from, I have never seen this before...

    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2146: syntax error : missing ',' before identifier 'name'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2065: 'name' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2065: 'T' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C3203: 'shared_ptr' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2146: syntax error : missing ',' before identifier 'Data'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2065: 'Data' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2143: syntax error : missing '>' before ';'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2208: 'std::map' : no members defined using this type
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 9 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    So yeah, any ideas? as far as I know I initialized everything right, except maybe the template portion for the shared pointers....

    Thanks for the help already
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    std::map<std::string name, std::vector<boost::shared_ptr<T>> Data;

    Separate the >> at the end with a space or it gets interpreted as the shift operator

    void Save_to_File(std::string Filename, std::map<std::string name, std::vector<boost::shared_ptr<T_>>)

    here too.
    Last edited by Darryl; 06-20-2007 at 02:35 PM.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Okay, thanks for that tip, but I'm getting all the same errors

    New code:

    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:	
    
    	std::map<std::string name, std::vector<boost::shared_ptr<T> > Data;
    	typedef boost::weak_ptr<Data> Data_Observer;
    	// 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
    	void Save_to_File(std::string Filename, 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, 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
    		Data_Observer Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Raw_Manager));
            // archive and stream closed when destructors are called
    	}
    private:
    	Manager_Map Database_Manager;
    };
    
    #endif
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    Code:
    std::map<std::string name, std::vector<boost::shared_ptr<T>> Data;
    Remove "name"

    EDIT: Also, what is T? Is your Database class supposed to be template <typename T> ?
    Last edited by brewbuck; 06-20-2007 at 02:50 PM.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Sweet, I should have caught that, thanks. 7 more errors....

    But this is really a question of what I'm trying to do...

    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2065: 'T' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C3203: 'shared_ptr' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2146: syntax error : missing ',' before identifier 'Data'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2065: 'Data' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2143: syntax error : missing '>' before ';'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : error C2208: 'std::map' : no members defined using this type
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(15) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 7 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I'm trying to make it so I can pass a map like that to one of my functions, only the shared pointer needs to be templated because I'm not sure exactly what I'm going to pass the function to save, or load.

    I suppose I need to make a templated function... but can someone help me with this?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    I'm trying to make it so I can pass a map like that to one of my functions, only the shared pointer needs to be templated because I'm not sure exactly what I'm going to pass the function to save, or load.
    But if you don't know the type (T) you can't instantiate an object. As it is now, you are trying to create one of these map objects in the Database class. You simply can't do that if you don't know what T is.

    Without knowing a lot more about how all this is supposed to work, I can't really suggest a fix.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    yeah I was just kinda testing out syntax, and failing horribly, I know I can't instantiate that map unless I know what T is, so I'll leave that up to other processes. But making a function be able to accept an ambiguous type is what I'm curious about.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    yeah I was just kinda testing out syntax, and failing horribly, I know I can't instantiate that map unless I know what T is, so I'll leave that up to other processes. But making a function be able to accept an ambiguous type is what I'm curious about.
    Then you need to make that function a template. For example, a function that can work on std::vectors of any type T:

    Code:
    template <typename T>
    void do_work(std::vector<T> &vec)
    {
        // do something to vec
    }
    In modern C++ you can have template member functions and template constructors, also.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Sweet, gotta love modern C++ then..

    Originally, I had my data objects setup so they *all* derived from a single object base class, I found that this had alot of type safety issues, so I decided to switch to templates, templates are a bit iffy in my realm of C++ knowledge but this should work better when it's finished.

    All the database class does is take a map of strings and shared pointers of an ambiguous type, attach a name to it, and write it to a file for later loading. I use weak pointers to observe the data via a vector of shared pointers to the maps, I think this will work just fine.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    All the database class does is take a map of strings and shared pointers of an ambiguous type, attach a name to it, and write it to a file for later loading.
    Note that this doesn't mean you can store any kind of shared pointer in a given map, though. You can only store shared pointers to type T. T can be any type, but each individual container can only contain a single type.

    If you really want to hold pointers to ANY kind of objects all in the same map, there's no other way but to use runtime polymorphism (i.e., the way you were doing it, with a common base class and virtual functions).

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    You mean I can't have a vector of pointers of maps of any typename?

    uh oh...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    You mean I can't have a vector of pointers of maps of any typename?
    To be clear, what you're asking for is a map that maps strings to pointers to all kinds of different objects? Different types of objects but all in the same map?

    You can do it, but the map isn't a template anymore, it just holds pointers to the base type:

    Code:
    std::map<std::string name, std::vector<boost::shared_ptr<base_class> > >
    Then, all types that might be in the map have to inherit from base_class.

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    No, not all in the same map.

    The vector is the unifying data type, it points to these maps that I create which are ambiguous, is this possible?

    you see, if I use a base class like object for all my data, I was told it would be very very bad for type safety
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shamino View Post
    No, not all in the same map.

    The vector is the unifying data type, it points to these maps that I create which are ambiguous, is this possible?
    I'm not sure what you mean here. Can you explain what you WANTED "T" to be in your original example?

    you see, if I use a base class like object for all my data, I was told it would be very very bad for type safety
    I'm not sure why somebody would tell you that. It sounds like what you want is polymorphism. There's no sense in trying to emulate polymorphism instead of just using it.

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    T could be a menu object, or a sound object, or a string object, yknow for long text portions in a text game. I want to organize all these data types in a centralized class, Database.

    If I use polymorphism here, it could be the end of my program if I try to use a sound file as a text file, know what I mean? If I use templates and return the actual specific data type, it prevents that problem, so stupid programmers don't try to add something to my system that will break it, not that they could know, and that is exactly the problem, they wouldn't know.

    Ogre3d does this and it is one of the main flaws of their engine. People try to program for ogre and end up breaking things because they accidentally grab a sound file and try to output it as a 3d model on the screen.

    It's really not a big big problem, but I'd rather do it this way, the type safe way.

    I want to have a vector in the database class, which points to maps, which are of type std::string/T, is that possible?
    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. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sreen Resolution Statistics?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 04-26-2004, 02:33 PM