0

我想用 C++ 制作一个 DVD 出租应用程序。我已经制作了客户类的基础知识,以及另一个保存客户 ID 的类,称为 CustomerDB。每个客户都有一个唯一的 ID。
我想在继续程序之前测试 CustomerDB,但是在编译程序时出现错误。

这是我写的代码:

头文件:

// DVD_App.h - Header File

#include <string>
#include <map>

using namespace std;

enum Status {ACTIVE, INACTIVE};

class Customer {
    private:
        string id;
        string name;
        string address;
        Status status;

    public:
        Customer (const string&, const string&, const Status);
        string &getId () { return id; }
};

class CustomerDB {
    private:
        static map<string, int> idList;

    public:
        static void addNewToIdList (const string &threeLetterOfName) {
            if (!doesThreeLettersOfNameExist(threeLetterOfName))
                idList.insert(pair<string, int>(threeLetterOfName, 0));
        }

        static bool doesThreeLettersOfNameExist (const string &threeLetterOfName) {
            map<string, int>::iterator i = idList.find(threeLetterOfName);
            if ((i->first).compare(threeLetterOfName) != 0)
                return false;
            return true;
        }

        static int nextNumber (const string &threeLetterOfName) {
            map<string, int>::iterator i = idList.find(threeLetterOfName);
            ++(i->second);
            return i->second;
        }
};

和源代码文件:

// DVD_App.cpp - C++ Source Code

#include <iostream>
#include <string>
#include "DVD_App.h"

using namespace std;

Customer::Customer (const string &cName, const string &cAddress, const Status cStatus) : name(cName), address(cAddress), status(cStatus) {
    string threeLetters = name.substr(0, 3);
    if (CustomerDB::doesThreeLettersOfNameExist(threeLetters))
        threeLetters += "" + CustomerDB::nextNumber(threeLetters);
    else {
        CustomerDB::addNewToIdList(threeLetters);
        threeLetters += "0";
    }
}

int main () {
    Customer k ("khaled", "beirut", ACTIVE);
    cout << k.getId() << endl;

    return 0;
}

我得到的错误是:

C:\Users\KiKo-SaMa\Desktop\C++>g++ DVD_App.cpp
C:\Users\KIKO-S~1\AppData\Local\Temp\ccSsS5HX.o:DVD_App.cpp:(.text$_ZN10Customer
DB14addNewToIdListERKSs[CustomerDB::addNewToIdList(std::basic_string<char, std::
char_traits<char>, std::allocator<char> > const&)]+0x59): undefined reference to
 `CustomerDB::idList'
C:\Users\KIKO-S~1\AppData\Local\Temp\ccSsS5HX.o:DVD_App.cpp:(.text$_ZN10Customer
DB27doesThreeLettersOfNameExistERKSs[CustomerDB::doesThreeLettersOfNameExist(std
::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x1
0): undefined reference to `CustomerDB::idList'
C:\Users\KIKO-S~1\AppData\Local\Temp\ccSsS5HX.o:DVD_App.cpp:(.text$_ZN10Customer
DB10nextNumberERKSs[CustomerDB::nextNumber(std::basic_string<char, std::char_tra
its<char>, std::allocator<char> > const&)]+0x10): undefined reference to `Custom
erDB::idList'
collect2: ld returned 1 exit status

我的程序有什么问题?

4

1 回答 1

7

您已声明静态变量 idList(在 .h 文件中),但未定义它(在 .cpp 文件中),例如

map<string, int> CustomerDB::idList;
int main () {
...
}
于 2012-04-27T09:10:56.520 回答