如何在不在类对象中重新定义类类型的情况下将数据类型定义为结构中的int

How do I define a datatype to be an int in a structure without redefining my class type in a class object?

本文关键字:定义 情况下 int 结构 类型 数据类型 对象 新定义      更新时间:2023-10-16

我很难解决重新定义错误。基本上,我在类头文件中有一个名为houseClassType的类对象,我还必须在结构头文件中使用houseClassType作为结构中数组的数据类型。以下是两个头文件:

内部头文件:

#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

Realtor头文件:

#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

任何帮助都将不胜感激。

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"?在一个好的设计中,它是结构还是类都无关紧要。

您的代码不明确。如果你有

class houseClassType;
typedef int houseClassType;

下面的代码是什么意思?

houseClassType x = new houseClassType();

您可以使用namespace来解决歧义,但最好更改第二个houseClassType类型和名称。

一个例子可能是这样的。

class House {
  public:
    enum class Type {
      ...
    }
};
相关文章: