C++ C++;类继承:函数

C++ C++;类继承:函数,c++,class,inheritance,compiler-errors,undeclared-identifier,C++,Class,Inheritance,Compiler Errors,Undeclared Identifier,我一直在为我的物理学位的编程模块做一些课程,但我遇到了一些麻烦。我必须创建一个名为Person的类和一个名为Employee的子类,以便: Person.hpp: #ifndef PERSON_HPP_ #define PERSON_HPP_ class Person { public: Person(const std::string & name="Anonymous"): name(name) {;} ~Person() {;} std::string

我一直在为我的物理学位的编程模块做一些课程,但我遇到了一些麻烦。我必须创建一个名为Person的类和一个名为Employee的子类,以便: Person.hpp:

#ifndef PERSON_HPP_
#define PERSON_HPP_

class Person {
public:
    Person(const std::string & name="Anonymous"): name(name) {;}
    ~Person() {;}

    std::string getname(){
        return name;
    }

    void setname(std::string newname) {
        name = newname;
    }

    void Print();

private:
    std::string name;
};

#endif /* PERSON_HPP_ */
Person.cpp:

void Person::Print(){
    std::string name = Person::getname;
    std::cout << name << std::endl;
}
#include <iostream>
#include <string>
#include "Person.hpp"
Employee.cpp:

void Employee::Print(){
    Person::Print();
    std::string job = Employee::getjob;
    std::cout << job << std::endl;
}
#include <iostream>
#include <string>
#include "Employee.hpp"
void Employee::Print(){
Person::Print();
std::string job=Employee::getjob;
std::cout您的错误在这里:

#include "Employee.cpp"
不要包含
.cpp
文件,将它们编译为链接阶段的单独输入


另外,不要忘记在
Employee.cpp
文件中包含“Employee.hpp”
!同样的情况也适用于
#类似地包含“Person.cpp”
等。

您的
包含应如下所示:

Person.cpp:

void Person::Print(){
    std::string name = Person::getname;
    std::cout << name << std::endl;
}
#include <iostream>
#include <string>
#include "Person.hpp"

而且不知道什么是
Employee
。include
Employee.hpp
通过引入
Employee
的定义解决了这个问题。

您包含了几个.cpp文件,而我猜您打算包含头文件

此外:

  Person::Print(); //this call is also wrong since Print() is not static
  std::string job = Employee::getjob;
getjob
是一个成员函数,调用成员函数时错过了
()
。同时,
getjob()
不是一个静态成员函数,它应该与类的一个对象绑定。您调用它的方式不正确。同样的错误发生在这里:

 std::string name = Person::getname; //inside Print() function of Person

#包括“Employee.cpp”
——这里是开始出错的地方。为了保持理智,您必须将接口与实现分开(decl/defn)。您的cpp文件应该包含它们需要包含的头文件。此外,您不希望使用此->
std::string job=Employee::getjob;
,您正在声明一个隐藏
job
成员的本地文件,只需使用
job
即可。
  Person::Print(); //this call is also wrong since Print() is not static
  std::string job = Employee::getjob;
 std::string name = Person::getname; //inside Print() function of Person