• Welcome to Valhalla Legends Archive.
 

std::map/struct Problems

Started by Sargera, June 28, 2004, 12:52 PM

Previous topic - Next topic

Sargera

Interestingly enough, you can't declare a map and a struct and then declare an object to that struct or use the map in the same global space you declared them in.  You have to do it in a local scope of whereever you can access the globally scoped struct/map.  The problem I'm having is if there is a way to fix this?  I thought it was kind of odd...Or is this just one of those things you must deal with?

A solution to my problem would just to pass the maps/structs by reference to the other several functions, but this is unsatisfactory IMO, and people already told me I should use it globally, but this problem occured.

Eibro

Quote from: Sargera on June 28, 2004, 12:52 PM
Interestingly enough, you can't declare a map and a struct and then declare an object to that struct or use the map in the same global space you declared them in.  You have to do it in a local scope of whereever you can access the globally scoped struct/map.  The problem I'm having is if there is a way to fix this?  I thought it was kind of odd...Or is this just one of those things you must deal with?

A solution to my problem would just to pass the maps/structs by reference to the other several functions, but this is unsatisfactory IMO, and people already told me I should use it globally, but this problem occured.
You're not making too much sense. Post an example. If I understand correctly, you should be able to do what you're asking.
Eibro of Yeti Lovers.

Sargera

#2
Sorry.


#include <map>
#include <iostream>
using namespace std;

struct user
{
   int flags;
   time_t lastseen;
};

map < char *, user > aMap;

user aUser;
aUser.flags = 6;
aUser.lastseen = 5;
aMap["test"] = aUser;

int main() {
   return 0;
}


That code is a compile-time error, refer to my first post.



#include <map>
#include <iostream>
using namespace std;

struct user
{
   int flags;
   time_t lastseen;
};

map < char *, user > aMap;

int main() {
   user aUser;
   aUser.flags = 6;
   aUser.lastseen = 5;
   aMap["test"] = aUser;
   return 0;
}


This code compiles fine, as you can see it's all used locally, not globally.  I don't understand why you can't use it globally too...Especially when it's declared globally too...I mean, you can use stuff you declare locally locally!

Sargera

#3
The reason why I want to use the map globally and add users to it is so I don't have to pass the map around by reference...just make it global to where all functions can access it in that file (there's many and they all relate to one thing hence in the same file).  If I added people locally in a function, it wouldn't work right unless I passed the map around...

Basically you add all the users into the map globally where you declare the map/struct and then use them locally in the functions.  The examples above are relatively useless and were used for example reasons.

Adron

#4
You can write declarations globally, but not code. Any code you write has to be in some kind of function.

Your second example that declares aMap globally looks fine. You can use the map from any function, just the way you want to. It's filled with data when main is run, which is the first thing that happens in your program.