c++ - 头文件中的类定义和包含字符串

标签 c++ class header-files

这是我当前的标题集合:

 #include "Header1.h"
 #include "BedAndMatress.h"
 #include "Sofa.h"
 #include "Table.h"
 #include iostream
 #include fstream
 #include iomanip 
 #include string
 #include cmath
 #include vector

using namespace std;


int main()

包含和命名空间标准位在我的主文件和我的“函数定义.cpp”文件中。但是,编译器抛出了一些错误:

  e:\computing\coursework2\programme.cpp(2) : fatal error C1083: Cannot open include
 file: 'BedAndMatress.h': No such file or directory

最初我在 Header1.h 文件中定义了我所有的类,但它提示文件和类定义的意外结束,所以我决定将它们分开。该文件包含在项目中,所有其他文件似乎都在工作,所以我不确定发生了什么。我还创建了一个名为 Bed 的新头文件,但它有同样的错误,所以我改变了它认为可能已经有一个具有该名称的标准文件(显然是长镜头)是否有头文件的最大数量?

此外,在类定义中,一些成员对象应该是字符串...

#ifndef CLASS_Bed
#define CLASS_Bed
//////BED
class Bed:public Item
{
string frame;
string frameColour;
string mattress;

public:

int Bed(int count);
int Bed(int count, int number, string name, string frm, string fclr, string mtres);
void count();
void printDetails();
}
#endif

但它无法识别类型说明符。

error C2501: 'string' : missing storage-class or type specifiers

我应该包括字符串吗?我在某处读到这可能会导致问题,所以如果这不是解决方案,我应该如何进行?

大大

最佳答案

should I include string? I read somewhere that this can cause issues so if thats not the solution how should i proceed?

您应该包括 <string> .你可能读到过不能把 using namespace std;在标题中,这是真的。但是,如果需要,包含标题也没有错。您需要限定 string 的使用虽然:

#ifndef CLASS_Bed
#define CLASS_Bed
//////BED
#include <string>
class Bed:public Item
{
std::string frame;
std::string frameColour;
std::string mattress;

public:

int Bed(int count);
int Bed(int count, int number, std::string name, std::string frm, std::string fclr, std::string mtres);
void count();
void printDetails();
};     //<-- note semi-colon here
#endif

关于c++ - 头文件中的类定义和包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211897/

相关文章:

c++ - 整数未被复制到对象中的数组

c++ - 如何在wxWIdgets中显示清晰的图标

c++ - 设计用于处理对象的管道

c++ - 在头类中创建内联函数到私有(private)变量

c++ - 避免 C++ 中重复的子类定义

c - 在代码块中使用 #include ""指令时出错

c++ - 创建不可复制对象的 vector

c++ - 将参数传递给一个类的构造函数,该类是另一个类的成员

python - 在旧式 python 类上使用属性会导致问题吗

c++ - 我应该使用简单的类还是高维矩阵?