2015-11-14 27 views
0

我很難解決重新定義錯誤。基本上,我在我的類頭文件中有一個名爲houseClassType的類對象,我也必須在我的結構頭文件中使用houseClassType作爲我的結構中的數組的數據類型。下面是兩個頭文件:如何在不重新定義類對象中的類類型的情況下將數據類型定義爲結構中的int?

房子的頭文件:

#include "Standards.h" 

#ifndef house_h 
#define house_h 

//Definition of class, house 
class houseClassType 
{ 
    //Data declaration section 
private: 
    int capacityOfGarage; 
    int yearBuilt; 
    int listingNumber; 

    double price; 
    double taxes; 
    double roomCounts[3]; 

    string location; 
    string style; 

    //Private method to set the county name 
    string SetCountyName(string); 
    string SetSchoolDistrictName(string); 

    //Private method to set school district name 
    void SetSchoolDistrictName(void); 

    //Set function for the object 
    void ExtractLocationData(string& state, string& county, string& city, 
    string& schoolDistrictName, string& address); 

    //Methods declaration 
public: 

    ///Default Constructor 
    houseClassType(void); 

    ///Get methods for data members - INLINE 
    int GetCapacity(void) { return capacityOfGarage; }; 
    int GetYearBuilt(void) { return yearBuilt; }; 
    int GetListingNumber(void) { return listingNumber; }; 

    double GetPrice(void) { return price; }; 
    double GetTaxes(void) { return taxes; }; 

    string GetLocation(void) { return location; }; 
    string GetStyle(void) { return style; }; 

    void GetRoomCounts(double[]); 

    //Set methods for data members 
    void SetCapacityOfGarage(int); 
    void SetYearBuilt(int); 
    void SetListingNumber(int); 

    void SetPrice(double); 
    void SetTaxes(double); 

    void SetLocation(string); 
    void SetStyle(string); 

    void SetRoomCounts(double[]); 

    //Output methods for data members 
    void OutputLocationData(ofstream&); 
    void OutputStyle(ofstream&); 
    void OutputRoomCounts(ofstream&); 
    void OutputCapacityOfGarage(ofstream&); 
    void OutputYearBuilt(ofstream&); 
    void OutputPrice(ofstream&); 
    void OutputTaxes(ofstream&); 
    void OutputListingNumber(ofstream&); 
    void OutputHouse(ofstream&); 

    ///Destructor 
    ~houseClassType(void); 

}; 

#endif 

房地產經紀人的頭文件:

#include "Standards.h" 

#ifndef Realtor_h 
#define Realtor_h 

const int NUMBER_OF_HOMES = 30; 

typedef int houseClassType; 

struct realtorStructType 
{ 
    string agentName; 


    houseClassType homes[NUMBER_OF_HOMES]; ///Redefinition error here 


    int numberOfHomes; 
}; 

void InputHomes(ifstream& fin, string agentName, int& numberOfHomes); 


#endif 

任何幫助將非常感激。

+1

您應該將代碼縮減到[mcve](http://stackoverflow.com/help/mcve)從所有這些代碼行中,只有3-4個與您的問題相關。 – bolov

+1

爲什麼你需要爲typedef使用同名的'houseClassType'?無論如何,如果你想使用相同名稱的不同類型,你需要使用命名空間。 – Barmar

+0

@Barmar你是對的,它不一定是一個typedef。但是,你能否給我一個你稱之爲「使用命名空間」來解決這個問題的例子? – Smiter

回答

1

C++語言喜歡在整個翻譯模塊中使用唯一的類型名稱。下面是不是唯一的類型名稱:

class houseClassType 
typedef int houseClassType; 

如果必須使用相同的名稱,那麼你就需要使用namespaces將它們分開:

namespace City 
{ 
    class houseClassType; 
} 
namespace Suburban 
{ 
    typedef int houseClassType; 
} 
struct realtorStructType 
{ 
    Suburban::houseClassType homes[MAX_HOMES]; 
}; 

我強烈建議你繪製或首先設計這個問題。這也會幫助你。

簡單的解決方案是使用不同的名稱。

另外,你的名字中是否需要後綴「ClassType」或「StructType」?在一個好的設計中,無論是結構還是類都不重要。

0

您的代碼不明確。如果你有

class houseClassType; 
typedef int houseClassType; 

什麼將下面的代碼是什麼意思?

houseClassType x = new houseClassType(); 

可以解決使用namespace的模糊性,但它更好地改變你的第二個houseClassType類型和名稱。

一個例子可能看起來像這樣。

class House { 

    public: 

    enum class Type { 
     ... 
    } 
}; 
+0

感謝您的回覆。看起來這個問題與類型defs或int數據類型無關。我完全應該將homes數組設置爲dataType houseClassType。我不斷收到錯誤信息,因爲我的realtor.h頭文件頂部沒有#include house.h文件名。謝謝回覆! – Smiter

+0

@Smiter你確定嗎?您上面提交的代碼不應被有效的編譯器接受。你仍然需要解決你的模糊問題。 – Jason

+0

我刪除了說「typedef int houseClassType;」的行並在realtor.h頭文件的頂部添加「#include house.h」。但是現在你提到它了,當我編譯主程序時,我遇到了c2019錯誤。 – Smiter

相關問題