2013-06-29 51 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