gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:58:32 25 4
gpt4 key购买 nike

我在解决重定义错误时遇到困难。基本上,我的类头文件中有一个名为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com