Error: Redefinition of Class (C++)

核能气质少年 提交于 2020-06-23 13:17:06

问题


I'm trying to figure out why I'm getting the following error:

error: redefinition of 'TimeDuration'

// TimeDuration.cpp

#define HOUR 3600
#define MIN 60

#include <iostream>
#include <string>
#include "TimeDuration.h"

using namespace std;

TimeDuration::TimeDuration() {
    seconds = 0;
}

void TimeDuration::setDuration(const int sec) {
    seconds = sec;
}

void TimeDuration::display() {
    // Some code to display the time
}

The error is showing in my header file.

// TimeDuration.h

class TimeDuration {
    private:
        int seconds;
    public:
        TimeDuration();                     
        void setDuration(const int sec);    
        void display();                     
};

回答1:


The error is probably because you don't have header guards in TimeDuration.h

A standard way to header guard is to at the beginning of the file write:

#ifndef TIME_DURATION_H
#define TIME_DURATION_H

and at the end of the file:

#endif


来源:https://stackoverflow.com/questions/33906418/error-redefinition-of-class-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!