Skip to main content
gamedev.net
🔒 Locked

Litle std::map issue...

Started by Impz0r 6 replies 1k views
Original Post
Impz0r
Impz0r
Hey everyone, As the topic said, i can''t compile the following class:
class foo
{
 public :
  typedef map<string, foo>  mofo;

  mofo   bar;
  
 public :
  foo () { } 
  ~ foo () { }

};
Thats what the compiler complains about: Vc7\include\utility(56) : error C2079: ''std:air<_Ty1,_Ty2>::second'' uses undefined class ''foo'' with [ _Ty1=const std::string, _Ty2=foo ] and i can''t get it..i tried alot to get it to work..but i failed I hope you guys could help me out of my misery Thanks in advance Impz0r
Stay Evil & Ugly!
Fruny
Fruny
a) Try adding a class foo; declaration before the class definition - though I believe a full declaration is needed if you are to use the class as a template parameter.

b) As a) + Try using a std::map pointer instead of a std::map instance. Delaying the instanciation may satisfy your compiler.

c) Store foo pointers instead of foo instances in the map.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Nervo
Nervo
The only way I could get your code to compile is by using a pointer to foo within map key-value pair. That sort of reminds me of when I made my doubly linked list class where I had a pointer to a struct within the struct that was meant to point to nodes before or after.

typedef map<string, foo*> mofo;



[edited by - nervo on January 30, 2004 8:42:00 PM]
Well, R2D22U2..
Impz0r
Impz0r
Thanks for your quick response

It seems that i have to use the ''pointer'' option to get it to work...weird.

Thanks anyway

Impz0r
Stay Evil & Ugly!
Trienco
Trienco
its funny, with the exception that i had
struct CommandNode {
map Child;
};

its just the same and... it compiles fine with intels compiler, as its smart enough to realize that the map will store its objects somewhere else and its absolutely unnecessary to know its exact size. unfortunately vc strictly sticks to the "cant use a class without it being fully defined".

too bad i cant stick with the intel compiler for longer than a few more days *g*, it would have made a really nice tree implementation, especially as it was the first time, that map creating a new entry if you access a non-existing key finally wasnt some annoying thing to be aware of but actually helping to make it a cleaner solution.

so this (the way its automagically creating the tree while traversing it is something i found kind of neat):
CommandNode* node=&CommandTree
string::iterator p=c.Name.begin();
while (p!=c.Name.end()) node=node->Child[*p++];

became this (again, only once i wish i could use a map without touching find() *sigh):


CommandNode* node=&CommandTree
string::iterator p=c.Name.begin();
while (p!=c.Name.end()) {
if (node->Child.find(*p) == node->Child.end())
node->Child[*p]=new CommandNode();
node=node->Child[*p++];
}


so just in case you have something like that you already know how to change it ,-)
f@dzhttp://festini.device-zero.de
Trienco
Trienco
quote:
Original post by Anonymous Poster
Why would you really want instances of your class within itself?



you dont.. but you might want instances of your class inside a container in the class. take the example above (im using mine, because i know what i wanted to achieve).

the console knows a bunch of commands, but instead of just letting you type and then see if its making any sense its traversing the tree with every key you press. lets pretend there are three commands: ransom, random, foo. the tree would look like this:


d-o-m
/
r-a-n
/ \
root s-o-m
\
f-o-o


each node is just a map, containing a char as key and another node. having something like above is making it extremely easy, especially to autocomplete what you type while theres just one child to choose.

as the map is initialy empty there arent problems arising from having the class inside itself.

while class C{C child};
is recursive and cant work
class C{map child};
is harmless. it doesnt matter if its the same or another class because you can control it.
class C{map child};
would just cause one more indirection, first to whereever map is storing its values and from there to the object itself. in the end its like
class C{C** child};
where you would ask: why would you want to store that as pointer to a pointer to an object?
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.