2012-04-27 97 views
0

我想用C++製作DVD租借應用程序。我已經完成了客​​戶類的基礎知識,以及另一個擁有名爲CustomerDB的客戶ID的類。每個客戶都有一個唯一的ID。
我想在進一步的程序之前測試CustomerDB,但編譯程序時出現錯誤。字符串和int的映射

這裏是我寫的代碼:

頭文件

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

什麼是w榮與我的程序?

回答

7

您已聲明靜態變量idList(在.h文件中),但未定義它(在.cpp文件中)

map<string, int> CustomerDB::idList; 
int main() { 
... 
} 
+1

是的,在一個cpp文件中。解釋是類中的靜態成員只是聲明,必須在其他地方實例化。 – 2012-04-27 09:11:32

+0

哦,好吧 謝謝chac和Agent_L – 2012-04-27 09:14:35