1

I've gone through all the threads related to static member variable here, but unfortunately that couldn't help me to find out the reason.

this is the problem:

  1. Define a class name dvd_db. Include the following members:

    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.

In main function use a DVD array and demonstrate a DVD shop. i.e. User can choose a DVD & buy it,
when a DVD is sold the Quantity goes down.

and, I've written this code to solve it. But facing problem while building this code. The compiler says undefined reference to everywhere I used the static variable cnt. One more question, as I want to set the cnt to 0 initially, how to do it since it's private variable?

and what can be done to solve the undefined reference problem?

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

3 Answers 3

3

So close! Just change

 //dvd_db::cnt=0;

To:

int dvd_db::cnt=0;

Why? A class has two parts: Declaration and definition. Usually the declaration goes in the .h file and the definition in the .cpp file. For various reasons cpp allows you to also put definitions of functions in the declaration. As you are doing, and which is fine for a single file example.

But this does not work for statics: Statics can have only one definition (by definition, haha), and it has to be outside the declaration.

In your class declaration you tell anybody looking at it "there is one and only one int cnt". Now you also have to place that int somewhere. And that is done outside the class declaration, and must be done only once.

Non-static members are allocated every time the class is instanciated, so they do not need a place before you make an instance of the class. Functions are a bit in between, they are code and therefore read-only. So the compiler can be smart about them. To put them in the class declaration allows the compiler to see them with the declaration and inline them. But big ones should be put outside of the declaration as well.

1
  • Good :) Please also consider accepting the answer if it was helpful, so the question gets closed.
    – starmole
    Commented Jun 29, 2013 at 9:08
1

One solution - already mentioned in the other answers - is add a definition (once, typically in a cpp file):

int dvd_db::cnt = 0;

But since c++17 there is another solution: static inline:

A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition

You can replace the declaration you have in the class definition (in the H file):

  static int cnt;

with:

//vvvvvvvvvvvvv--------vvvv-
  static inline int cnt = 0;

This way you don't need a separate definition in a cpp file.

0

the solution is quite simple ... For static variables you need to specify the instance once outside:

  class Foo {

  private:
         static int a;

  };

  int Foo::a = 0;

  int main() {
         Foo foo;
  }

So in your case all you need to do is uncomment the line // dvd_db::cnt = 0;

and put an int in front of it:

 int dvd_db::cnt = 0;

that's it.. your linking issues will be solved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.