1

I have a map with string and an class pointer. I am from JAVA so please guide me.

class Client {

public:
    static map<string, Client*> CLIENTS;
    Client(string dir);
    static Client* get_client(string name);
    virtual ~Client();
};

My get_client method is

Client* Client::get_client(string name) {
       map<string, Client*>::iterator it = CLIENTS.find(name);
}

gives me the error below

Undefined symbols for architecture x86_64:
  "Client::CLIENTS", referenced from:
      Client::get_client(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in client.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Kathick
  • 1,395
  • 5
  • 19
  • 30

2 Answers2

2

You need to define the CLIENTS which you have only declared. So outside of class Client{, in your clients.cpp file add

 std::map<std::string, Client*> Client::CLIENTS;

BTW, I find the name CLIENTS quite confusing. Maybe it should be client_dict

Don't forget to compile with debugging information and all warnings, e.g. with g++ -Wall -g on Linux. Improve your code till no warnings are given (your get_client will get some). And learn how to use the debugger (e.g. gdb on Linux).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Add

map<string, Client*> Client::CLIENTS;

to cpp file. And do not omit std:: prefix before STL classes. Especially in header

borisbn
  • 4,988
  • 25
  • 42