Reputation: 79
I've made a class named Client and the definition goes below in client.h:
#ifndef __CLIENT_H__
#define __CLIENT_H__
#include <string>
using namespace std;
class Client {
public:
Client(string n, string p) : name(n), password(p) {logged_in = true;}
void set_name(string n, string p) {name=n; password=p; }
private:
string name;
string password;
bool logged_in;
};
#endif
then I've inherited a singleton class from Client named Admin in admin.h:
#ifndef __ADMIN_H__
#define __ADMIN_H__
#include "client.h"
using namespace std;
class Admin : public Client {
public:
static Admin* get_admin(string n, string p)
{
if (Admin::n == 0)
Admin::admin = new Admin(n, p);
else
Admin::admin->set_name(n, p);
return Admin::admin;
}
private:
Admin(string n, string p) : Client(n, p) {Admin::n++;}
static int n;
static Admin* admin;
};
int Admin::n = 0;
#endif
and here is my main.cpp:
#include <iostream>
#include <string>
#include "client.h"
#include "admin.h"
using namespace std;
int main()
{
Admin* administrator = Admin::get_admin("ghamar", "utcom90");
}
and the problem is that when I want to compile main.cpp using g++ (g++ main.cpp), I get the following error:
/tmp/cc15VnHc.o: In function `Admin::get_admin(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
main.cpp:(.text._ZN5Admin9get_adminESsSs[Admin::get_admin(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x5f): undefined reference to `Admin::admin'
main.cpp:(.text._ZN5Admin9get_adminESsSs[Admin::get_admin(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0xa0): undefined reference to `Admin::admin'
main.cpp:(.text._ZN5Admin9get_adminESsSs[Admin::get_admin(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0xd1): undefined reference to `Admin::admin'
the important part of the error says:
undefined reference to 'Admin::admin'
Can anybody help me out with this? thanks a lot for your time.
Upvotes: 1
Views: 1266
Reputation: 4939
You need to define Admin
outside the class since it is static.
Admin* Admin::admin = NULL;
Upvotes: 1
Reputation: 62995
You have two problems here, and are only seeing the errors for one so far.
You need to define Admin::n
and Admin::admin
in a single source file. Currently the former is defined in a header (which will cause multiple-definition errors once more than one source file includes that header), and the latter is not defined at all.
Remove the definition of Admin::n
from admin.h, and add the following to a single source file:
#include "admin.h"
int Admin::n = 0;
Admin* Admin::admin = 0;
Upvotes: 2
Reputation: 254751
As well as declaring the static members in the class definition, you also need to define them in exactly one source file:
#include "admin.h"
int Admin::n = 0;
Admin* Admin::admin = 0;
You should remove the definition of n
from the header; that will give "multiple definition" errors if you include the header from more than one source file. The = 0
bits are optional, since static objects are zero-initialised by default.
You should also remove the underscores from the start of the include guards; those are reserved, and using them could cause conflicts with names used by the standard library implementation.
Upvotes: 2
Reputation: 122011
You have declared admin
, but not defined it (yet you have defined n
):
int Admin::n = 0;
Admin* Admin::admin; // <-- add this
Upvotes: 1