简体   繁体   English

错误:重新定义 Class (C++)

[英]Error: Redefinition of Class (C++)

I'm trying to figure out why I'm getting the following error:我试图弄清楚为什么会出现以下错误:

error: redefinition of 'TimeDuration'错误:重新定义“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.错误显示在我的 header 文件中。

// TimeDuration.h

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

The error is probably because you don't have header guards in TimeDuration.h该错误可能是因为您在 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

you can use您可以使用

#pragma once

in your TimeDuration.h file, at the starting在您的 TimeDuration.h 文件中,在开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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