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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.

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