错误:C2146:语法错误:缺少';'在标识符'm_Employer'之前,

时间:2015-09-15 04:04:51

标签: c++ qt inheritance header composite

随着错误“C4430缺少类型说明符int假设”x2和C2061语法错误雇主         代码:

Person.h

    #ifndef PERSON_H
    #define PERSON_H
    #include "employer.h"
    #include "position.h"
    #include <QTextStream>
    #include <QString>

    class Person
    {
    private:
        QString m_Name;
        bool m_Employed;

在此处获取语法错误并缺少类型说明符:

        Employer m_Employer;
        Position m_Position;
    public:
        Person();
        Person(QString name);
        QString toString();

语法错误

        void setPosition(Employer &newC, Position &newP);
        void getPosition();
        void getEmployer();
    };

    #endif // PERSON_H

Person.cpp

    #include "person.h"

    Person::Person()
    {
        m_Name = "";
    }

    Person::Person(QString name)
    {
        m_Name = name;
    }

    QString Person::toString()
    {
        return "";
    }

    void Person::setPosition(Employer &newC, Position &newP)
    {

    }

Employer.h文件:

    #ifndef EMPLOYER_H
    #define EMPLOYER_H
    #include "person.h"
    #include <QTextStream>
    #include<QString>


    class Employer
    {
    private:
        QString m_Name;
        QString m_Market;

    public:
        Employer();
        Employer(QString name, QString market);

语法错误

        bool hire(Person &newHire, Position pos);
        QString toString();
    };

    #endif // EMPLOYER_H

Employer.cpp

    #include "employer.h"

    Employer::Employer()
    {

    }

    Employer::Employer(QString name, QString market)
    {
        m_Name = name;
        m_Market = market;
    }

    QString toString()
    {
     return "";
    }

Position.h文件:

    #ifndef POSITION_H
    #define POSITION_H
    #include <QTextStream>
    #include <QString>

    class Position
    {
    private:
        QString m_Name;
        QString m_Description;

    public:
        Position();
        Position(QString name, QString description);
        ~Position();
        QString toString();
    };

    #endif // POSITION_H

Position.cpp文件:

    #include "position.h"

    Position::Position()
    {
        m_Name = "";
        m_Description = "";
    }
    Position::Position(QString name, Qstring description)
    {
         m_Name = name;
         m_Description = description;
    }

    QString Position::toString()
    {
      return "";
    }

我也有一个主要但它没有被使用,我想在再出现之前解决这些问题 我一直在看这个,我似乎无法找出什么是错的,请非常感谢任何帮助,谢谢你提前。

1 个答案:

答案 0 :(得分:3)

你有一个循环依赖。 Person.h包括Employer.h,其中包含Person.h

您可以在Person中转发声明Employer.h

class Person;添加到Employee.h并将#include "person.h"移至Employee.cpp

错误来自于编译器编译Employer.cpp(或Person.cpp无关紧要),它包含Employer.h,它告诉编译器包括Person.h,它告诉编译器包含Employer.h,但这次包含警卫启动并停止再次包含Employer.h的内容。所以它开始编译Person.h而没有提到Employer。因此,当它看到Employer m_employer时,它是一种未知类型。