简体   繁体   English

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

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

I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). 我有一个类,它将处理我之前创建的另一个类的对象数组(工作正常)。 The problem appears when I try to create an object of my List-class. 当我尝试创建List-class的对象时出现问题。

This is the header of the 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

This is the main function: 这是主要功能:

#include <iostream>
#include "PersonList.h"

using namespace std;

int main() {

PersonList personlist;

return 0;   
}

The error my compiler is giving me is the following: 我的编译器给出的错误如下:

error: "27 \\PersonList.h ISO C++ forbids declaration of `Personlist' with no type" 错误:“27 \\ PersonList.h ISO C ++禁止声明`Personlist'没有类型”

I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. 我已经搜索了答案,但由于我对C ++很陌生,所以有点令人困惑,我还没有找到合适的答案。 It would be great if you could explain this error for me. 如果你能为我解释这个错误会很棒。

You have the wrong capitalisation on your constructor declaration. 您的构造函数声明上的大小写错误。 You have Personlist(); 你有Personlist(); but need PersonList(); 但需要PersonList(); . Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type. 因为你拥有的不等于类名,所以它被认为是函数而不是构造函数,函数需要返回类型。

Do not add your own types to the standard namespace( std ), instead create your own namespace and define your class inside it. 不要将自己的类型添加到标准命名空间( std ),而是创建自己的命名空间并在其中定义类。

//PersonList.h //PersonList.h

namespace PersonNamespace 
{
    class PersonList 
    {
        //members here
    };
}

//Main.cpp //Main.cpp

using namespace PersonNamespace;

The actual error is that you made a typo in Personlist instead of PersonList 实际错误是您在Personlist了拼写错误而不是PersonList

The error is because you got the capitalisation wrong when you declared the constructor; 该错误是因为在声明构造函数时,大小写错误; it should be PersonList() not Personlist() . 它应该是PersonList()而不是Personlist()

Also, you should never declare your own classes in the std namespace; 此外,您不应该在std命名空间中声明自己的类; that's reserved for the standard library. 这是为标准库保留的。 You shoud make up your own namespace name, and put your things in that. 你应该组成自己的命名空间名称,然后把你的东西放进去。

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

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