1

我创建了一个名为 Client 的类,定义如下 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

然后我在 admin.h 中从名为 Admin 的客户端继承了一个单例类:

#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

这是我的 main.cpp:

#include <iostream>
#include <string>
#include "client.h"
#include "admin.h"

using namespace std;

int main()
{
    Admin* administrator = Admin::get_admin("ghamar", "utcom90");
}

问题是,当我想使用 g++ (g++ main.cpp) 编译 main.cpp 时,出现以下错误:

/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'

错误的重要部分说:

undefined reference to 'Admin::admin'

有人可以帮我解决这个问题吗?非常感谢您的时间。

4

4 回答 4

2

除了在类定义中声明静态成员外,您还需要在一个源文件中定义它们:

#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.

于 2012-05-17T15:30:23.780 回答
2

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;
于 2012-05-17T15:31:11.343 回答
1

您已声明admin,但未定义它(但您已定义n):

int Admin::n = 0; 
Admin* Admin::admin;    // <-- add this
于 2012-05-17T15:29:19.880 回答
1

您需要Admin在类外部定义,因为它是静态的。

Admin* Admin::admin = NULL;
于 2012-05-17T15:29:52.567 回答