C++ 错误:C+中类的重新定义和以前的类定义+;
当我尝试使用classC++ 错误:C+中类的重新定义和以前的类定义+;,c++,oop,C++,Oop,当我尝试使用classcounter.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 defin
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.h
LowerBoundedCounter.h
和UpperBoundedCounter.h
都包括Counter.h
文件BoundedCounter.h
包括LowerBoundedCounter.h
和UpperBoundedCounter.h
文件。实现文件为counterApp.cpp
(此处未提供)
这是柜台。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
\ifndef计数器
#定义计数器
#包括
班级计数器{
私人:
int-val;
公众:
计数器():val(0){}
计数器(int val):val(val){}
作废{
这->val++;
}
作废{
这->瓦尔--;
}
int getVal()常量{
返回此->val;
}
friend std::ostream&operator您的头保护宏在所有头文件中都是错误的
#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H
输入错误'R'
UpperBoundedCounter.h中存在一个较小的问题,它没有关闭#endif。但这将导致另一个问题。我看不出问题,但我建议查看预处理器的输出以获取线索。g++-E counterApp.cpp
忘记了#endif
不是较小的问题,程序格式不正确EN
#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
#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
#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H