解释错误:ISO C++禁止声明没有类型的"人员列表"

Explain the error: ISO C++ forbids declaration of `Personlist' with no type

本文关键字:类型 列表 错误 ISO C++ 声明 禁止 解释      更新时间:2023-10-16

我有一个类,它将处理我之前创建的另一个类的对象数组(它工作良好)。当我尝试创建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)中添加您自己的类型,而是创建您自己的命名空间并在其中定义您的类。

//PersonList.h

namespace PersonNamespace 
{
    class PersonList 
    {
        //members here
    };
}
//Main.cpp

using namespace PersonNamespace;

实际错误是您在Personlist而不是PersonList中打错字

这个错误是因为你在声明构造函数时大写错误;应该是PersonList(),而不是Personlist()

同样,你不应该在std命名空间中声明你自己的类;这是为标准库保留的。您应该创建自己的命名空间名称,并将您的东西放在其中。