|  | Improper use of std::map? |  | |
| | | Hakusa@gmail.com |  |
| Posted: Thu Sep 04, 2008 8:16 am Post subject: Improper use of std::map? |  |
| |  | |
OK, I have the bellow code which give me an error (see the comment).
#pragma once
#include <map> #include <string> using std::string;
class Actor { public: unsigned int hp;
Actor( const unsigned int _hp ) : hp( _hp ) { }
Actor() : hp( 0 ) { }
virtual ~Actor();
virtual void act() = 0; };
typedef std::map<string,Actor*> ActT; typedef ActT::iterator ActIt; ActT actors;
class Player : public Actor { public: Player( const unsigned int _hp ) : Actor( _hp ) { }
Player() : Actor( 0 ) { }
void act(); };
// TESTING // actors[ "Player1" ] = new Player( 100 ); // ERROR: Expected conversion, constructor, destructor before "=".
class Enemy : public Actor { public: void act(); };
Is this not proper use of the sdt::map? I was thinking it's EXACTLY the right use (or that it's exactly what I want to use), but I can't figure out this error. Thanks in advance.
{ Isn't it because the expression statement is in the namespace scope? Enclose it in a function body, and that gives me no error. -mod/sk }
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Zara |  |
| Posted: Thu Sep 04, 2008 9:28 am Post subject: Re: Improper use of std::map? |  |
| |  | |
On Thu, 4 Sep 2008 02:16:46 CST, "Hakusa@gmail.com" <Hakusa@gmail.com> wrote:
| Quote: | OK, I have the bellow code which give me an error (see the comment).
#pragma once
#include <map #include <string using std::string;
class Actor ...
typedef std::map<string,Actor*> ActT;
|
You should use a shared_ptr instead of a raw pointer to be sure the resources are not lost, and are copiable.
| Quote: | typedef ActT::iterator ActIt; ActT actors;
class Player : public Actor ...
// TESTING // actors[ "Player1" ] = new Player( 100 ); // ERROR: Expected
|
This line is outside any function. That is the reason of the error: Map is initialized at definiton, but individual elements must be initialized within a function
<...>
| Quote: | Is this not proper use of the sdt::map? I was thinking it's EXACTLY the right use (or that it's exactly what I want to use), but I can't figure out this error. Thanks in advance.
See above |
best regards,
Zara
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|