该类已定义,但该程序声称它已重新定义

时间:2015-04-29 17:37:51

标签: c++

所以我有这个错误,就像标题一样,它是类型重定义错误。我查看了之前关于此类错误的帖子并尝试使用他们的解决方案;但是他们都没有工作。

这是employee.h的头文件

class Employee { 
public:
    Employee();
    void getHours();
    void getSalary();
    void getVacationDays();
    void getVacationForm();
};

这就是employee.cpp

#include "employee.h"
#include <iostream>
#include <string>
using namespace std;
void Employee::getHours(){
    cout << " I work 40 hours per week\n";
};
void Employee::getSalary(){
    cout << " I earn $40, 000\n";
};
void Employee::getVacationDays(){
    cout << " I receive 2 weeks vacation\n";
};
void Employee::getVacationForm(){
    cout << " Use the yellow vacation form \n";
};

之前我已经运行过这些文件并且它们工作得非常好,但是当我创建一个派生类时,“Marketer”程序在某个时刻突然中断。

这是marketer.h

#include <iostream>
#include "employee.h"
using namespace std;
class Marketer: public Employee
{
public:
    Marketer();
    void Marketer::getPay();
    void Marketer::getTime();
    void Marketer::getDayOff();
    void Marketer::getForm();
};

这是Marketer.cpp

#include <iostream>
#include "Marketer.h"
#include <string>
using namespace std;
void Marketer::getTime(){
    cout << " I work 40 hours per week\n";
};
void Marketer::getPay(){
    cout << " I earn $50, 000\n";
};
void Marketer::getDayOff(){
    cout << " I receive 2 weeks vacation\n";
};
void Marketer::getForm(){
    cout << " Use the yellow vacation form \n";
};

当我运行程序时,它只会给我一个错误说

"Error C2011: 'Employee' :'class' type is redefined". 

我发现employee.h / .cpp和Marketer.h / .cpp都没有错误 有谁知道出了什么问题?

1 个答案:

答案 0 :(得分:0)

你需要确保你的.h只被解析一次。 #pragma once或#ifndef myheader_h ...