繁体   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