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

标签 c++

我在解决重定义错误时遇到困难。基本上,我的类头文件中有一个名为 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”?在一个好的设计中,无论是结构还是类都没有关系。

关于c++ - 如何在不重新定义类对象中的类类型的情况下将数据类型定义为结构中的 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33712689/

相关文章:

c++ - 内联非成员函数内的本地类使用 MSVC2005 生成 LNK2005

c++ - 在C++中使用变量而不是 `#define`指定数组大小是否不好? (C错误: variably modified at file scope) [closed]

c++ - 模板函数如何根据类型在编译时修改它的行为?

c++ - 是否有任何完全符合 C++ 的实现?

c++ - 使用 new 分配静态数组

c++ - 虚拟函数的STK回调振荡器问题

c++ - 如何在 ssd-caffe 中加载图像而不是 LMDB

c++ - 使用 exec() 执行 perl/python/... 脚本

c++ - Qt 5.5 QOpenGLTexture 复制数据问题

C++11 vector 的智能指针