2016-10-28 19 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â 

を取得し、私は二回、いくつかのクラスを含めておりますことを承知しています、私はそれを見つける方法を知らない。私が間違っていることを見つけるのを助けてくれますか? Counter.h, LowerBoundedCounter.h, UpperBoundedCounter.h,BoundedCounter.hの4つのクラスがあります。 LowerBoundedCounter.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クラスは最後に、私は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 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; 
       } 

}; 

ですすべてのヘッダーファイル。

#ifndef BOUNDED_COUNTER_H 
#define BOUNDER_COUNTER_H 

タイポ 'R'

決算#endifのを持っていないUpperBoundedCounter.h、中に小さな問題があります。しかし、これは別の問題を引き起こすでしょう。

+0

忘れてしまった '#endif'は小さい問題ではなく、プログラムは悪い形で作られています – Danh

+0

私はそれを見落としました!それを指摘してくれてありがとう! – Avi

関連する問題