2016-10-28 50 views
-2

當我嘗試用類counter.cpp運行的類,我收到以下錯誤階級和階級的原定義的重新定義錯誤:在C++

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類。這個類包含一個'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 
+0

我沒有看到問題,但我建議看看預處理器輸出的線索。 'g ++ -E counterApp.cpp' – nephtes

+0

重複我不能。 – user4581301

回答

1

你的頭部保護宏是錯誤的所有頭文件。

#ifndef BOUNDED_COUNTER_H 
#define BOUNDER_COUNTER_H 

錯字 'R'

有一個在UpperBoundedCounter.h,不具有封閉#ENDIF較小的問題。但是這會造成另一個問題。

+0

忘了'#endif'是不是一個小問題,程序不正常,然後 – Danh

+0

我忽略了!感謝您指出! – Avi