错误:类的重新定义(C ++) [英] Error: Redefinition of Class (C++)

查看:253
本文介绍了错误:类的重新定义(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么出现以下错误:

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

错误:重新定义"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();                     
};

推荐答案

该错误可能是因为您在TimeDuration.h中没有标题保护.

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

并在文件末尾:

#endif

这篇关于错误:类的重新定义(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆