繁体   English   中英

解释错误:ISO C ++禁止声明没有类型的`Personlist'

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

我有一个类,它将处理我之前创建的另一个类的对象数组(工作正常)。 当我尝试创建List-class的对象时出现问题。

这是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;   
}

我的编译器给出的错误如下:

错误:“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命名空间中声明自己的类; 这是为标准库保留的。 你应该组成自己的命名空间名称,然后把你的东西放进去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM