c++求大神帮我看看为什么这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'
源程序代码如下://BaseShape.h文件#include<iostream>usingnamespacestd;classBaseShape{public:Base...
源程序代码如下:
// BaseShape.h文件
#include <iostream>
using namespace std;
class BaseShape {
public:
BaseShape() {};
virtual ~BaseShape() {};
virtual void DrawShape() = 0;
};
//*********************************
//ShapeFactory.h文件
#include <fstream>
#include <string>
#include "Line.h"
#include "BaseShape.h"
//定义工厂类
class ShapeFactory{
public:
ShapeFactory();
~ShapeFactory();
//BaseShape* SelectShape(string);
BaseShape* SelectShape(string shape) {
if(shape=="Line" || shape=="线")return new Line();
else cout<< "输入的图形名称错误!" <<endl;
}
};
// ShapeFactory.cpp文件
#include "ShapeFactory.h"
ShapeFactory::ShapeFactory(void)
{}
ShapeFactory::~ShapeFactory(void)
{}
//********************************************
// Line.h文件
///////////////////直线Line/////////////////////
#include "BaseShape.h"
class Line : public BaseShape {
private:
int n;
static int L;
public:
Line() {};
virtual ~Line() {};
//构造函数成员初始化列表,构造函数之后,函数体之前。
Line ( int n) : n(n) { }
//覆盖抽象类的纯虚函数
virtualvoid DrawShape();
};
// Line.cpp文件
#include "Line.h"
//绘制直线
void Line ::DrawShape() {
int n;
static int L;
cout<< "请选择直线长度: " <<endl;
cin>> n;
for(int i=0; i<n; i++) {
cout<< "*" ;
}
L++;
cout<<endl<< "^_^ 您已经绘制"<< L << "条直线^_^ "<<endl;
}
//********************************************
**********************************************
*********************************************
#include <fstream>
#include <vector>
#include <string>
#include "Line.h"
#include "BaseShape.h"
#include "ShapeFactory.h"
#include <iostream>
using namespace std;
int main() {
/*
读infile类 的文件ShapeFactoryFile.txt中可绘制的图形并为每行加上"行号-",输出到运行屏幕上。
*/
ifstream infile("ShapeFactoryFile.txt");
if(!infile) {//检验文件是否成功打开
cout<< "sorry! Unable to open ShapeFactoryFile. " <<endl;
return -1;
}
string line;
vector<string> text;
while (getline(infile,line)) {
text.push_back(line);
}
for(int j=0; j<text.size(); j++)
cout<< j+1 << " - " << text[j] <<endl;
cout<<endl << "*************以上是可以绘制的图形名***************" <<endl<<endl;
//定义工厂对象、基类指针
ShapeFactory factory;
BaseShape * pBaseShape;
string shape;
int t=0;
while(true) {
cout<<"请输入图形(英文或中文)名称(以#为结束程序): ";
cin>>shape;
if(shape=="#") break;
BaseShape* pBaseShape=factory.SelectShape(shape);//选择具体类
pBaseShape->DrawShape();//调用具体类的画法
delete pBaseShape;
t++;
cout<< "您总共已经输出"<< t <<"个图形。" <<endl;
}
return 0;
} 展开
// BaseShape.h文件
#include <iostream>
using namespace std;
class BaseShape {
public:
BaseShape() {};
virtual ~BaseShape() {};
virtual void DrawShape() = 0;
};
//*********************************
//ShapeFactory.h文件
#include <fstream>
#include <string>
#include "Line.h"
#include "BaseShape.h"
//定义工厂类
class ShapeFactory{
public:
ShapeFactory();
~ShapeFactory();
//BaseShape* SelectShape(string);
BaseShape* SelectShape(string shape) {
if(shape=="Line" || shape=="线")return new Line();
else cout<< "输入的图形名称错误!" <<endl;
}
};
// ShapeFactory.cpp文件
#include "ShapeFactory.h"
ShapeFactory::ShapeFactory(void)
{}
ShapeFactory::~ShapeFactory(void)
{}
//********************************************
// Line.h文件
///////////////////直线Line/////////////////////
#include "BaseShape.h"
class Line : public BaseShape {
private:
int n;
static int L;
public:
Line() {};
virtual ~Line() {};
//构造函数成员初始化列表,构造函数之后,函数体之前。
Line ( int n) : n(n) { }
//覆盖抽象类的纯虚函数
virtualvoid DrawShape();
};
// Line.cpp文件
#include "Line.h"
//绘制直线
void Line ::DrawShape() {
int n;
static int L;
cout<< "请选择直线长度: " <<endl;
cin>> n;
for(int i=0; i<n; i++) {
cout<< "*" ;
}
L++;
cout<<endl<< "^_^ 您已经绘制"<< L << "条直线^_^ "<<endl;
}
//********************************************
**********************************************
*********************************************
#include <fstream>
#include <vector>
#include <string>
#include "Line.h"
#include "BaseShape.h"
#include "ShapeFactory.h"
#include <iostream>
using namespace std;
int main() {
/*
读infile类 的文件ShapeFactoryFile.txt中可绘制的图形并为每行加上"行号-",输出到运行屏幕上。
*/
ifstream infile("ShapeFactoryFile.txt");
if(!infile) {//检验文件是否成功打开
cout<< "sorry! Unable to open ShapeFactoryFile. " <<endl;
return -1;
}
string line;
vector<string> text;
while (getline(infile,line)) {
text.push_back(line);
}
for(int j=0; j<text.size(); j++)
cout<< j+1 << " - " << text[j] <<endl;
cout<<endl << "*************以上是可以绘制的图形名***************" <<endl<<endl;
//定义工厂对象、基类指针
ShapeFactory factory;
BaseShape * pBaseShape;
string shape;
int t=0;
while(true) {
cout<<"请输入图形(英文或中文)名称(以#为结束程序): ";
cin>>shape;
if(shape=="#") break;
BaseShape* pBaseShape=factory.SelectShape(shape);//选择具体类
pBaseShape->DrawShape();//调用具体类的画法
delete pBaseShape;
t++;
cout<< "您总共已经输出"<< t <<"个图形。" <<endl;
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询