2014-04-20 28 views
1

我想通過使用靜態函數來顯示類的對象和對象的數量。我輸入了這段代碼,但它不起作用。它給出了一個錯誤Too many types indeclaration" and "undefined symbol getCount。誰能幫我?這段代碼中的錯誤實際在哪裏?面向對象編程,使用靜態函數來計數對象

#include<iostream> 
#include<string> 

class Bag { 
private: 
    static int objectCount; 
    int Weight; 
    std::string Brand; 
    std::string Type; 
    std::string Material; 
    std::string Colour; 
public: 
    Bag(int W, std::string B, std::string T, std::string M, std::string C) { 
     Weight = W; 
     Brand = B; 
     Type = T; 
     Material = M; 
     Colour = C; 
     objectCount++; 
    } 

    void print() { 
     std::cout << "\n"; 
     std::cout << "Bag: \n\n"; 
     std::cout << "Weight:\t\t" << Weight << "kg" << '\n'; 
     std::cout << "Brand:\t\t" << Brand << '\n' << "Type:\t\t" << Type << '\n'; 
     std::cout << "Material:\t" << Material << '\n' << "colour:\t\t" << Colour << std::endl; 
    } 

    static int getCount() { 
     return objectCount; 
    } 
}; 

int Bag::objectCount = 0; 

int main() { 
    Bag bag_1(2, "Slazanger", "Atheletic Bag", "Polyethylene", "Brown"); 
    bag_1.print(); 
    std::cout << "object count " << Bag::getCount() << '\n'; 

    Bag bag_2(4, "Samsonite", "Travel Bag", "Synthetic Fibre", "Gray"); 
    bag_2.print(); 
    std::cout << "object count " << Bag::getCount() << '\n'; 

    Bag bag_3(5, "Herschel", "Duffel bag", "Leather", "Black"); 
    bag_3.print(); 
    std::cout << "object count " << Bag::getCount() << '\n'; 

    Bag bag_4(3, "Kewin Woods", "Hand Bag", "Fibre", "Blue"); 
    bag_4.print(); 
    std::cout << "object count " << Bag::getCount() << std::endl; 

    while(!std::cin.get()); 
    return 0; 
} 
+0

這應該是什麼語言? – niklasfi

+1

我想你錯過了一個關閉'}'請確保正確格式化你的代碼,以便出現這些類型的問題。 – niklasfi

+0

這是一個使用Borland(編譯器)C++的OPP。 – user3351862

回答

0

這是你的代碼的工作版本:

#include <iostream> 
#include <cstring> 

using namespace std; 

class Bag { 
private: 
    static int objectCount; 
    int Weight; 
    string Brand, Type, Material, Colour; 

public: 
    Bag(int W, string B, string T, string M, string C) //constructor 
    { 
    Weight = W; 
    Brand = B; 
    Type = T; 
    Material = M; 
    Colour = C; 

    objectCount++; 
    } 
    void print() { 
    cout << "\n"; 
    cout << "Bag: \n\n"; 
    cout << "Weight:\t\t" << Weight << "kg" << endl; 
    cout << "Brand:\t\t" << Brand << endl << "Type:\t\t" << Type << endl; 
    cout << "Material:\t" << Material << endl << "colour:\t\t" << Colour 
     << endl; 
    } 
    static int getCount() //static function to count objects 
    { 
    cout << objectCount; 
    }; 
}; 


int Bag::objectCount = 0; 
int main() { 

    Bag bag_1(2, "Slazanger", "Atheletic Bag", "Polyethylene", "Brown"); 

    Bag bag_2(4, "Samsonite", "Travel Bag", "Synthetic Fibre", "Gray"); 
    Bag bag_3(5, "Herschel", "Duffel bag", "Leather", "Black"); 
    Bag bag_4(3, "Kewin Woods", "Hand Bag", "Fibre", "Blue"); 
    bag_1.print(); 
    cout << "object count" << Bag::getCount(); 
    bag_2.print(); 
    cout << "object count" << Bag::getCount(); 

    bag_3.print(); 
    cout << "object count" << Bag::getCount(); 

    bag_4.print(); 
    cout << "object count" << Bag::getCount(); 
} 

有在您發佈的版本幾個錯誤:包括當

    用C
  • ++,你不需要.h文件
  • 您使用的是cout而沒有std::限定符或將using namespace std;添加到你的來源。另請參閱this
  • 您的靜態函數未聲明/類定義內部定義
  • 應該int main而不是void main

最後一個音符:我刪除了你的#include <conio.h>這或許應該閱讀#include <conio>getch因爲我編這個在Linux機器上。隨意添加他們,如果你想要他們。

+0

我不確定這是否在C++ 84中是真的(這是OP最可能編程的),但使用構造函數初始化器列表比默認初始化然後賦值要容易得多。這不是你的建議,因爲我確信你知道它,只是爲了讓OP能理解。 – noobProgrammer

+0

沒有'cconio'作爲微軟特定的工具,所以它只是conio.h'。只有來自C標準庫的頭文件被作爲帶有'c'前綴的無擴展文件引入。 –

1

您正在確定範圍,getCount靜態範圍爲翻譯 單位,而不是類。因此它沒有可用的符號objectCount

要解決這個問題,只需將該方法放入課堂。

class Bag { 
private: 
    static int objectCount; 
    int Weight; 
    string Brand,Type,Material,Colour; 
public: 
    Bag(int W ,string B ,string T,string M,string C) 
    { 
     Weight=W; 
     Brand=B; 
     Type=T; 
     Material=M; 
     Colour=C; 

     objectCount++; 
    } 

    void print() 
    { 
     cout<<"\n"; 
     cout<<"Bag: \n\n"; 
     cout<<"Weight:\t\t"<<Weight<<"kg"<<endl; 
     cout<<"Brand:\t\t"<<Brand<<endl<<"Type:\t\t"<<Type<<endl; 
     cout<<"Material:\t"<<Material<<endl<<"colour:\t\t"<<Colour<<endl; 
    } 

    static int getCount() 
    { 
     cout<< objectCount; 
    } 
}; 

的方法,另外,Borland公司是一個非常古老的編譯器和驚訝,甚至還 聽到它的名字,最後一個版本是大約15年前那麼你真的應該 考慮使用clanggccmsvc和升級你的學習教材 不古老的東西。在實踐方面有很多進化,標準和編譯器一致性。

例如,C++頭文件沒有擴展名,還有其他一些小東西。