解释错误:ISO C++禁止声明没有类型的"人员列表"
Explain the error: ISO C++ forbids declaration of `Personlist' with no type
我有一个类,它将处理我之前创建的另一个类的对象数组(它工作良好)。当我尝试创建List-class的对象时,问题出现了。
这是列表类的头文件:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
这是主要功能:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
编译器给我的错误如下:
error: "27 Personlist .h ISO c++禁止声明' Personlist'没有"
"
我已经搜索了答案,但由于我对c++很陌生,它有点令人困惑,我还没有找到任何合适的答案。如果你能给我解释一下这个错误就太好了。
构造函数声明的大写错误。你有Personlist();
,但需要PersonList();
。因为所拥有的不等于类名,所以它被认为是函数而不是构造函数,并且函数需要返回类型。
不要在标准命名空间(std
)中添加您自己的类型,而是创建您自己的命名空间并在其中定义您的类。
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp using namespace PersonNamespace;
实际错误是您在Personlist
而不是PersonList
中打错字
这个错误是因为你在声明构造函数时大写错误;应该是PersonList()
,而不是Personlist()
。
同样,你不应该在std
命名空间中声明你自己的类;这是为标准库保留的。您应该创建自己的命名空间名称,并将您的东西放在其中。
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 测试两个类型列表中的所有组合
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- 在迭代模板类型列表时无法停止递归
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- 阅读从 istream 到矢量的不同类型列表
- 如何在C++中创建类型列表的 n 路笛卡尔积?
- c++17通过生成预先声明的类型列表的笛卡尔乘积来生成std::变体
- 将变量模板限制为类型列表
- 我可以使用特征指定变体的类型列表吗?
- 如何对要用于模板参数的类型列表进行编码
- 创建类型列表并访问每种类型的静态成员?
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 如果创建支持返回可变参数类型列表的通用模板 API,我应该使用 std::tuple 还是其他东西?
- std::仅移动类型列表:不能在 VC++ 中放入 std::vector
- C++类型列表创建子列表
- 调用STD ::功能,并带有变体类型列表
- 在没有宏的情况下在 C++98 中创建类型列表时遇到问题
- 如何从类型列表重建参数包