簡體   English   中英

C ++類靜態成員變量錯誤

[英]C++ class static member variable error

我在這里瀏覽了與靜態成員變量相關的所有線程,但是不幸的是,這無法幫助我找出原因。

這就是問題:

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;
}

很近! 只是改變

 //dvd_db::cnt=0;

至:

int dvd_db::cnt=0;

為什么? 一個類有兩個部分:聲明和定義。 通常,聲明位於.h文件中,而定義位於.cpp文件中。 由於各種原因,cpp允許您還將函數的定義放入聲明中。 正如您在做的那樣,這對於單個文件示例來說很好。

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

在您的類聲明中,您告訴任何查看它的人“只有一個int cnt”。 現在,您還必須將該int放置在某個地方。 這是在類聲明之外完成的,並且只能執行一次。

每次實例化該類時都會分配非靜態成員,因此在創建該類的實例之前,它們不需要位置。 函數介於兩者之間,它們是代碼,因此是只讀的。 因此,編譯器可以很聰明地對待它們。 將它們放在類聲明中,使編譯器可以通過聲明查看它們並內聯它們。 但是,大文件也應放在聲明之外。

解決方案非常簡單...對於靜態變量,您需要在外部指定一次實例:

  class Foo {

  private:
         static int a;

  };

  int Foo::a = 0;

  int main() {
         Foo foo;
  }

因此,您需要做的只是取消注釋行// dvd_db :: cnt = 0;

並在其前面放置一個int:

 int dvd_db::cnt = 0;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM