错误:在C ++中重新定义类和以前的类定义

时间:2016-10-28 02:21:10

标签: c++ oop

当我尝试使用类counter.cpp运行该类时,出现以下错误

In file included from Bounded_Counter.h:7:0,
                 from counterApp.cpp:4:
Lower_Bounded_Counter.h:9:7: error: redefinition of âclass LowerBoundedCounterâ
Lower_Bounded_Counter.h:9:7: error: previous definition of âclass LowerBoundedCounterâ
In file included from Bounded_Counter.h:8:0,
                 from counterApp.cpp:4:
Upper_Bounded_Counter.h:9:7: error: redefinition of âclass UpperBoundedCounterâ
Upper_Bounded_Counter.h:9:7: error: previous definition of âclass UpperBoundedCounterâ

我知道我有两次上课,但我不知道如何找到它。你能帮我找一下我做错的事吗?有4个班级Counter.h, LowerBoundedCounter.h, UpperBoundedCounter.h,BoundedCounter.hLowerBoundedCounter.hUpperBoundedCounter.h都包含Counter.h文件。 BoundedCounter.h包含LowerBoundedCounter.hUpperBoundedCounter.h个文件。实施文件为counterApp.cpp(此处未提供)

这是Counter.h类。

#ifndef COUNTER_H
#define COUNTER_H

#include <iostream>

class Counter{
        private:
                int val;
        public:
                Counter():val(0) {}

                Counter(int val):val(val){}

                void up(){
                        this->val++;
                }

                void down(){
                        this->val--;
                }

                int getVal() const {
                        return this->val;
                }

                friend std::ostream &operator <<(std::ostream &os, const Counter &counter) {
                        os << "A Counter with a value of " << counter.val;
                        return os; 
                }

};

#endif

这是LowerBoundedCounter.h类。这个课程包含一个&#39; Counter&#39;对象

#ifndef LOWER_BOUNDED_COUNTER_H
#define LOWER_BOUNDER_COUNTER_H

#include<iostream>

#include "Counter.h"


class LowerBoundedCounter{
        private:
                Counter counter;
                int limit;
        public:
                LowerBoundedCounter(int limit,int val):counter(val), limit(limit){

                }

                LowerBoundedCounter(int val):counter(val),limit(10){

                }

                LowerBoundedCounter():counter(),limit(0){

                }

                void up(){
                        if(getVal() >  limit){
                                counter.up();
                        }
                }

                void down(){
                        counter.down();
                }

                int getLimit() const{
                        return this->limit;
                }

                int getVal() const{
                        return counter.getVal();
                }

                friend std::ostream &operator <<(std::ostream &os, const LowerBoundedCounter &LowerBoundedCounter){
                        os << "An LowerBoundedCounter with a value of " << LowerBoundedCounter.getVal() << " and a limit of "<<LowerBoundedCounter.limit;
                        return os;
                }

};

#endif

这里是UpperBoundedCounter.h类

#ifndef UPPER_BOUNDED_COUNTER_H
#define UPPER_BOUNDER_COUNTER_H

#include<iostream>

#include "Counter.h"

class UpperBoundedCounter{
        private:
                Counter counter;
                int limit;
        public:
                UpperBoundedCounter(int limit,int val):counter(val), limit(limit){

                }

                UpperBoundedCounter(int val):counter(val),limit(10){

                }

                UpperBoundedCounter():counter(),limit(10){

                }

                void up(){
                        if(getVal() < limit){
                                counter.up();
                        }
                }

                void down(){
                        counter.down();
                }

                int getLimit() const {
                        return this->limit;
                }

                int getVal() const{
                        return counter.getVal();
                }

                friend std::ostream &operator <<(std::ostream &os, const UpperBoundedCounter &UpperBoundedCounter){
                        os << "An UpperBoundedCounter with a value of " << UpperBoundedCounter.getVal() << " and a limit of "<<UpperBoundedCounter.limit;
                        return os;
                }

};

最后,我在BoundedCounter.h

中有来自上述所有3个类的对象
#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H

#include<iostream>

#include "Counter.h"
#include "Lower_Bounded_Counter.h"
#include "Upper_Bounded_Counter.h"

class BoundedCounter{
        private:
                Counter counter;
                UpperBoundedCounter upperBoundedCounter;
                LowerBoundedCounter lowerBoundedCounter;
        public:
                BoundedCounter(int val, int upperLimit, int lowerLimit):counter(val),upperBoundedCounter(upperLimit),lowerBoundedCounter(lowerLimit){

                }


                BoundedCounter(int val,int upperLimit):counter(val), upperBoundedCounter(upperLimit), lowerBoundedCounter(){

                }

                BoundedCounter(int val):counter(val),upperBoundedCounter(),lowerBoundedCounter(){

                }

                BoundedCounter():counter(), upperBoundedCounter(), lowerBoundedCounter(){

                }

                void up(){
                        if(getVal() < upperBoundedCounter.getLimit() && getVal() > lowerBoundedCounter.getLimit()){
                                counter.up();
                        }
                }

                void down(){
                        counter.down();
                }

                int getLowerLimit() const{
                        return lowerBoundedCounter.getLimit();
                }

                int getUpperLimit() const{
                        return upperBoundedCounter.getLimit();
                }

                int getVal() const{
                        return counter.getVal();
                }

                friend std::ostream &operator <<(std::ostream &os, const BoundedCounter &BoundedCounter){
                        os << "An BoundedCounter with a value of " << BoundedCounter.getVal() << " and a lower limit of "<<BoundedCounter.getLowerLimit() 
                                                                                              <<" and a higher limit of "<<BoundedCounter.getUpperLimit();
                        return os;
                }

};

#endif

1 个答案:

答案 0 :(得分:1)

您的标头保护宏在所有标头文件中都是错误的。

#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H

Typo&#39; R&#39;

UpperBoundedCounter.h中存在一个较小的问题,它没有关闭#endif。但这会引起另一个问题。