What is wrong with that class?

Started by
2 comments, last by Khao 15 years, 8 months ago
Hey folks, I'm facing a weird problem here. I'm trying to make a 'static' class that will contain my maps and hash tables for users, since they exist only once in the program and they need to be accessible from any place of the code I though static was the best way to do this. Although when I include it from more than 1 file, I get 'Lists' has not been declared errors. I can't see what's wrong in this code because I have another static class called MysqlManager which is included in many files and I do not receive the error for the MysqlManager class with only static members. Here's List.hpp

#ifndef LISTS_HPP
#define LISTS_HPP

#include <map>
#include <ext/hash_map>
#include <google/dense_hash_map>
#include "User.hpp"

struct eqstr
{
   bool operator()(const char* s1, const char* s2) const
   {
      return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
   };
};

class Lists
{
public:
   static dense_hash_map<const char*, int, __gnu_cxx::hash<const char*>, eqstr> UsernameToUID;
   Lists(){};
   static void initialize()
   {
      UsernameToUID.set_empty_key("---");
   };

   static map<int, User*> UIDtoUser;
};

map<int, User*> Lists::UIDtoUser;
dense_hash_map<const char*, int, __gnu_cxx::hash<const char*>, eqstr> Lists::UsernameToUID(10000);

#endif

I need to include this class into many files because the Server, Lobby and User classes need to have access to Lists::UsernameToUID or Lists::UIDtoUser. My MysqlManager class is the same as this one, with static members only and I can include it in different files with no problem.. Anybody knows why I get this error?
Advertisement
You need to move the definition of the static members (i.e. the last 2 lines of code there) into the 'lists.cpp' file. otherwise they are being redefined in every file you included 'lists.hpp' into.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by Khao
I'm trying to make a 'static' class that will contain my maps and hash tables for users, since they exist only once in the program and they need to be accessible from any place of the code

Not really a big deal here, but do they? Why does your sound subsystem need to know about the maps? Why do you need access to the maps when reading input from the player? "It needs to be accessible everywhere" is usually a sign of bad design.

But like I said, probably not a big deal here. Just thought I'd mention that you're basing your decisions on a false assumption. These maps do not need to be accessible everywhere.
Right, it's really easy for me to restructure the other class to not use Lists, but I am still wondering as why does my MysqlManager class can be included in many other files, and Lists cannot :\

This topic is closed to new replies.

Advertisement