如何在不在类对象中重新定义类类型的情况下将数据类型定义为结构中的int
How do I define a datatype to be an int in a structure without redefining my class type in a class object?
我很难解决重新定义错误。基本上,我在类头文件中有一个名为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 {
...
}
};
相关文章:
- 这个c++代码是如何在没有定义函数的情况下运行的
- A类的定义需要B类的定义,如何在不公开B的定义的情况下公开A的公共函数?
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 如何在不给它任何类型(如整数)的情况下定义某物?
- 在 C++ 中,我可以在不修改类的情况下定义对类的隐式转换吗?
- 可以在没有构建代码的情况下转到定义吗?
- msgpack:在不知道类型的情况下解包自定义类
- C++-在没有自定义.lib文件的情况下从Lua C模块调用Lua函数
- 如何定义可以在没有联合的情况下容纳uintptr_t或uint32_t的类型?
- 如何在不使用 vector::erase() 的情况下编写自定义 Vector 方法来删除元素?
- C++:如何在不定义的情况下声明非函数?
- 如何在没有动态内存分配(堆)的情况下为可变大小数组(矢量)定义安全容器或视图
- 如何在自定义删除器的情况下复制unique_ptr
- 如何在不取消定义类型的情况下为 C++ 中的类型编写 getter?
- 是否可以在不使用"undef"的情况下重新定义宏?
- 如何在<typename> <long> 不更改给定代码的情况下将自定义向量与 STL 向量相互转换?
- 是否有另一种方法可以在不使用宏定义的情况下进行此操作
- 为什么C++没有简洁的语法允许在不定义计数器的情况下多次执行操作?
- 是否可以超载[]运算符在不定义类的情况下访问特定的字符
- 在不提供定义的情况下重写函数有什么意义?