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

阿维

当我尝试使用该类运行该类时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.h并且UpperBoundedCounter.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类。此类包含一个“ Counter”对象。

#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
尼基(Z. Nyeki)

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

#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H

输入“ R”

UpperBoundedCounter.h中存在一个较小的问题,该问题没有结束符#endif。但这将引起另一个问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章