C++类静态成员变量错误

标签 c++ class static

我在这里已经遍历了所有与静态成员变量相关的线程,但不幸的是,这无法帮助我找出原因。

问题是:

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.

在 main 函数中使用一个 DVD 阵列并演示一个 DVD 商店。即用户可以选择 DVD 并购买, 当 DVD 售出时,数量会下降。

而且,我已经编写了这段代码来解决它。但是在构建此代码时遇到问题。编译器说对我使用静态变量 cnt 的所有地方都 undefined reference 。还有一个问题,我一开始想把cnt设置为0,既然是私有(private)变量怎么办?

如何解决 undefined reference 问题?

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 放在某个地方。这是在类声明之外完成的,而且只能完成一次。

每次实例化类时都会分配非静态成员,因此在创建类实例之前不需要放置它们。函数介于两者之间,它们是代码,因此是只读的。所以编译器可以对它们很聪明。将它们放在类声明中允许编译器通过声明查看它们并将它们内联。但是大的也应该放在声明之外。

关于C++类静态成员变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17378228/

相关文章:

c++ - 为什么我不能打印二叉树?

php - 如果 body class 是 home,使用 php 显示不同的标题 Logo 图像?

c++ - 工作 C++03 代码上的 G++ (C++14) 链接器错误

javascript - 使用静态 get 获取未定义

c++ - CUDA NPP - GPU 错误检查时出现未知错误

c++ - Visual Studio C++ 参数缩进

c++ - 如何仅匹配括号之间的嵌套组?

c++ - 在 C++ 中具有相同类的属性

java - Java中如何指定主类

c++ - 动态库的链接问题