简体   繁体   中英

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.

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

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. It would be great if you could explain this error for me.

You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need 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.

//PersonList.h

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

//Main.cpp

using namespace PersonNamespace;

The actual error is that you made a typo in Personlist instead of PersonList

The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist() .

Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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