Author | Message | Time |
---|---|---|
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. | June 28, 2004, 5:52 PM |
Eibro | [quote author=Sargera link=board=5;threadid=7478;start=0#msg67601 date=1088445140] 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. [/quote]You're not making too much sense. Post an example. If I understand correctly, you should be able to do what you're asking. | June 28, 2004, 6:15 PM |
Sargera | Sorry. [code] #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; } [/code] That code is a compile-time error, refer to my first post. [code] #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; } [/code] 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! | June 28, 2004, 6:22 PM |
Sargera | 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. | June 28, 2004, 8:42 PM |
Adron | 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. | June 30, 2004, 10:29 PM |