2016-05-15 51 views
0

我是編程的初學者,開始使用C++。我有問題使用我的靜態變量。我讀了關於在各種相同的問題中使用靜態變量,但我明白這只是Car :: countOfInput;。從下一篇:在C++中使用靜態和靜態函數的錯誤

  1. How do I call a static method of another class

  2. C++ Static member method call on class instance

  3. How to call static method from another class?

這是我的代碼:

#include <iostream> 
#include <conio.h> 
#include <string.h> 

using namespace std; 

class Car{ 
    private: 
     static int countOfInput; 
     char *carName; 
     double carNumber; 
    public: 
     Car() { 
      static int countOfInput = 0; 
      char carName = {'X'}; 
      double carNumber = 0; 
     } 

     void setVal(){ 
      double number; 

      cout << "Car Name: "; 
      char* str = new char[strlen(str) + 1]; 
      cin>>str; 
      strcpy(carName, str); 

      cout << endl << "Car Number: "; 
      cin >> number; cout << endl; 
      carNumber = number; 

      Car::countOfInput += 1; 
     } 

     friend void print(){ 
      if(Car::countOfInput == 0){ 
       cout << "Error: empty!"; 
       return; 
      } 
      cout << "LIST OF CarS" << endl; 
      cout << "Car Name: " << carName << "\t"; 
      cout << "Car Number: " << carNumber << endl; 
     } const 

     void setCarNumber(int x){carNumber = x;} 
     int getCarNumber(){return carNumber;} 

     void setcarName(char x[]){strcpy(carName, x);} 
     char getcarName(){return *carName;} 

     int getCountOfInput(){return countOfInput;} 
     void setCountOfInput(int x){countOfInput = x;} 
}; 

int main(){ 
    Car product[3]; 
    product[0].setVal(); 
    product[0].print(); 

    getch(); 
    return 0; 
} 

當我運行此:

F:\CLion\practise\main.cpp: In function 'void print()':

F:\CLion\practise\main.cpp:10:13: error: invalid use of non-static data member 'Car::carName' char *carName; ^

F:\CLion\practise\main.cpp:40:33: error: from this location cout << "Car Name: " << carName << "\t"; ^

F:\CLion\practise\main.cpp:11:12: error: invalid use of non-static data member 'Car::carNumber' double carNumber; ^

F:\CLion\practise\main.cpp:41:35: error: from this location cout << "Car Number: " << carNumber << endl; ^

F:\CLion\practise\main.cpp: In function 'int main()':

F:\CLion\practise\main.cpp:57:16: error: 'class Car' has no member named 'print' product[0].print();

我用的克利翁,在此先感謝。

+1

如果一個類變量被標記爲'static',則它不與其類的_any_實例關聯。你確定這就是你想要的嗎? – erip

+0

粗略地說,我希望每次調用setVal()函數時增加我的靜態變量** count **。 –

+2

我認爲你需要採取更小的步驟。你已經在這裏建立了至少4個單獨的錯誤 - 編寫更少的代碼和測試早些時候會簡化這個。 –

回答

3

Car()內部的變量聲明是局部變量,而不是成員變量的初始化。要初始化成員變量,只是這樣做(這是一個member initializer list):

Car() : carName("X"), carNumber(0) {} 

和放countOfInput定義在.cpp文件中類外(在全球範圍內):

int Car::countOfInput = 0; 

(如果你想每次Car()構造函數被調用時重置countOfInput0,你可以做的是,在構造函數體:countOfInput = 0;

+0

Ooooo,很好的與當地人交流。 :)看起來像來自學校的測驗問題。 – erip

+0

謝謝,所有的錯誤都沒有了。 –

+0

如果我想讓我的'print'功能成爲'friend',我該怎麼辦? –

2

錯誤消息與靜態變量或靜態方法無關。

關鍵字friend位於print()的前面,使其成爲非成員函數(注意最後一條錯誤消息),然後無法直接訪問成員變量。根據用法它應該是一個成員函數,所以只需刪除關鍵字friend

而作爲@tuple_cat建議,const應該放在開頭{之前。

void print() const { 
    if(Car::countOfInput == 0){ 
     cout << "Error: empty!"; 
     return; 
    } 
    cout << "LIST OF CarS" << endl; 
    cout << "Car Name: " << carName << "\t"; 
    cout << "Car Number: " << carNumber << endl; 
} 
+1

並把'const'放在開頭'{'。 – emlai

+0

沒有'friend'甚至'const',甚至在beginninng之前放置'const',還沒有工作。 –

+0

給我這個錯誤:_F:/CLion/practise/main.cpp:31:對Car :: countOfInput'_的未定義引用。 –

2

哦,親愛的。你有一些問題。首先,你的構造函數不會初始化任何成員變量。

Car() { 
     static int countOfInput = 0; 
     char carName = {'X'}; 
     double carNumber = 0; 
    } 

相反,它聲明瞭三個局部變量並將它們設置爲值。你想要的是:

Car() 
     : carName(nullptr) 
     , carNumber(0.0) 
    { 
    } 

然後setValue是複製串入內存指向由this->carName,但那是未初始化的,所以可以在任何地方。另外,strlen(str)在初始化str之前註定會失敗。讓carName爲std :: string,那麼你就需要構建它,代碼變得:

Car() 
     : carNumber(0.0) 
    { 
    } 

    void setVal(){ 

     cout << "Car Name: "; 
     cin >> carName; 

     cout << endl << "Car Number: "; 
     cin >> carNumber; cout << endl; 

     Car::countOfInput += 1; 
    } 

接下來,你需要做print不是朋友(並使其常量)。

void print() const { 
    ... 

最後您需要定義coutOfInput。你已經宣佈了它,但你也需要一個定義。在任何函數之外:

int Car::countOfInput = 0;