2013-06-29 153 views
1

我已經通過了所有與靜態成員變量相關的線程,但不幸的是,這無法幫助我找出原因。C++類靜態成員變量錯誤

這是問題:

1)定義一個類名稱DVD_DB。包括以下成員。

DATA MEMBERS: 
Name of DVD – private character array, size 10 
Price – private double variable 
Quantity – private int variable 
A private static int variable to keep track of how many DVD have been created so far. 
MEMBER FUNCTIONS: 
To assign initial values 
To set price and quantity 
To get price and quantity 
To Print DVD 
To display how many DVD has been created so far. 

在主要功能使用DVD陣列和顯示DVD商店。即用戶可以選擇購買DVD &, 當DVD出售數量下降。

和,我已經寫了這個代碼來解決它。但在構建此代碼時遇到問題。編譯器說對我使用靜態變量cnt的每一處都有未定義的引用。還有一個問題,因爲我想最初將cnt設置爲0,因爲它是私有變量,所以要怎麼做?

可以做些什麼來解決未定義的參考問題?

class dvd_db{ 

private: 
    string name; 
    float price; 
    int quantity; 
    static int cnt; 

public: 


    dvd_db() 
    { 
     name=""; 
     price=0; 
     quantity=0; 
     cnt++; //to count the total number of dvds 
    } 

    dvd_db(string str,float p,int q) 
    { 
     name = str; 
     price = p; 
     quantity = q; 
     // cnt=0; 
     cnt+=q; 
    } 

    void set_name(string str) 
    { 
     name = str; 
    } 
    string get_name(void) 
    { 
     return name; 
    } 
    void set_price(float p) 
    { 
     price = p; 
    } 
    float get_price(void) 
    { 
     return price; 
    } 
    void set_quantity(int q) 
    { 
     quantity = q; 
     cnt+=q; 

    } 
    int get_quantity(void) 
    { 
     return quantity; 
    } 
    void show_info(void) 
    { 
     cout<<"Name if the DVD: "<<name<<endl; 
     cout<<"Price: "<<price<<endl; 
     cout<<"Available Quantity: "<<quantity<<endl; 

    } 
    void total(void) 
    { 
     cout<<"Total number of dvd is: "<<cnt<<endl; 
    } 

    void buy(void) 
    { 
     if(quantity>0){ 
      quantity--; 
      cnt--; 
      cout<<"Thanks for purchasing this item"<<endl; 

     } 
     else 
      cout<<"This Item can not be bought."<<endl; 

    } 
}; 
//dvd_db::cnt=0; 

int main() 
{ 
    dvd_db dvd[3]; 

    int i,j,k,n; 

    dvd[0].set_name("A Beautiful Mind"); 
    dvd[0].set_price(50.00); 
    dvd[0].set_quantity(10); 

    dvd[1].set_name("October Sky"); 
    dvd[1].set_price(50.00); 
    dvd[1].set_quantity(15); 

    dvd[2].set_name("Shawshank Redemption"); 
    dvd[2].set_price(50.00); 
    dvd[2].set_quantity(100); 



    cout<<"Welcome to Banani International Movie House"<<endl; 
    cout<<"Enter the serial no. to buy an item, -1 to view total no. of dvd(s), or enter 0 to quit."<<endl; 
    cout<<"Here is our collection:"<<endl<<endl<<endl<<endl; 

    for(i=0; i<3; i++){ 
     cout<<"serial no. "<<i+1<<endl; 
     cout<<"------------------------------------"<<endl; 
     dvd[i].show_info(); 

    } 

    cout<<"Enter: "<<endl; 

    while(cin>>n) 
    { 
     if(n==-1){ 
      dvd[0].total(); 
      cout<<"Enter: "<<endl; 
      continue; 
     } 
     dvd[n-1].buy(); 
     cout<<"Enter: "<<endl; 
    } 
    return 0; 
} 
+0

可能的重複[未定義引用靜態類成員](http://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member) – juanchopanza

回答

0

的解決方案很簡單...對於靜態變量,你需要一次外指定實例:

class Foo { 

    private: 
     static int a; 

    }; 

    int Foo::a = 0; 

    int main() { 
     Foo foo; 
    } 

所以你的情況,所有你需要做的是取消對該行 // dvd_db :: cnt = 0;

,並把它前面一個int:

int dvd_db::cnt = 0; 

就是這樣..你的鏈接問題將得到解決。

2

太近了!只要改變

//dvd_db::cnt=0; 

要:

int dvd_db::cnt=0; 

爲什麼?一個類有兩個部分:聲明和定義。通常聲明進入.h文件並在.cpp文件中定義。出於各種原因,cpp允許您在聲明中添加函數的定義。正如你所做的那樣,對於一個單獨的文件示例來說是很好的。

但這不適用於靜態:靜態只能有一個定義(按定義,哈哈),它必須在聲明之外。

在你的類聲明中,你告訴任何人看它「只有一個int cnt」。現在你也必須把int放在某個地方。這是在類聲明之外完成的,並且只能進行一次。

非靜態成員會在每次創建類時被分配,所以在創建類的實例之前,他們不需要一個地方。函數介於兩者之間,它們是代碼,因此是隻讀的。所以編譯器可以很聰明。將它們放入類聲明允許編譯器通過聲明查看它們並將它們內聯。但是重要的也應該放在宣言之外。

+0

謝謝,這解決了我的問題。 – prodhan

+0

好:)請也考慮接受答案,如果它是有幫助的,所以問題被關閉。 – starmole