c++求大神帮我看看为啥这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++求大神帮我看看为啥这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'相关的知识,希望对你有一定的参考价值。
源程序代码如下:
// 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;
每个类的头文件最开始都加上
#pragma once本回答被提问者采纳
以上是关于c++求大神帮我看看为啥这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'的主要内容,如果未能解决你的问题,请参考以下文章
设置oracle11g定时自动备份,为啥没有备份?请大神们帮我看看为啥!