这个程序最后应该如何正确输出……

Aha_程序猿 2015-06-03 06:26:42
/*main.cpp*/]
#include<iostream>
#include<vector>
#include<algorithm>
#include"CShap.h"
#include"Compare.h"
using namespace std;
vector<CShap *> pShapes;
int main()
{
int i, n;
CRectangle* pr;
CCricle* pc;
CTriangle *pt;
cout << "Please enter the number of geometry: " << endl;
cin >> n;
for (i = 0; i < n; i++){
char c;
cout << "Please enter the sharp you want to calculate:" << endl << "(R:Rectangle T:Triangle C:Cricle)" << endl;
cin >> c;
switch (c){
case 'R':
pr = new CRectangle();
cout << "Please enter the width and high: " << endl;
cin >> pr->width >> pr->high;
pShapes.push_back(pr);
break;
case 'T':
pt = new CTriangle();
cout << "Please enter sides: " << endl;
cin >> pt->a >> pt->b >> pt->c;
pShapes.push_back(pt);
break;
case 'C':
pc = new CCricle();
cout << "Please enter radius: " << endl;
cin >> pc->r;
pShapes.push_back(pc);
break;
}
}
sort(pShapes.begin(), pShapes.end(),Compare);
vector<CShap *>::iterator it;
for (it = pShapes.begin(); it != pShapes.end(); it++){
*it;
}
system("pause");
return 0;
}


/*CShap.h*/
#include<iostream>
#include<math.h>
using std::cout;
using std::endl;
class CShap{
public:
virtual double Area() = 0;
virtual void PrintInfo() = 0;
};
class CRectangle :public CShap{
public:
double width, high;
virtual double Area();
virtual void PrintInfo();
};
class CCricle :public CShap{
public:
double r;
virtual double Area();
virtual void PrintInfo();
};
class CTriangle :public CShap{
public:
double a, b, c;
virtual double Area();
virtual void PrintInfo();
};
double CRectangle :: Area(){
return width*high;
}
void CRectangle::PrintInfo(){
cout << "Rectangle: " << Area() << endl;
}
double CCricle::Area(){
double pi = 3.141592653;
return pi*r*r;
}
void CCricle::PrintInfo(){
cout << "Cricle: " << Area() << endl;
}
double CTriangle ::Area(){
double p = (a + b + c) / 2.0;
return sqrt(p*(p - a)*(p - b)*(p - c));
}
void CTriangle::PrintInfo(){
cout << "Triangle: " << Area() << endl;
}



/*Compare.cpp*/
#include"CShap.h"
bool Compare(const void*s1, const void*s2)
{
double a1, a2;
CShap**p1;
CShap**p2;
p1 = (CShap**)s1;
p2 = (CShap**)s2;
a1 = (*p1)->Area();
a2 = (*p2)->Area();
if (a1 < a2){
return 1;
}
else{
return 0;
}
}



/*CShap.h*/
#ifndef CShap_h
#define CShap_h
#include<iostream>
#include<math.h>
using std::cout;
using std::endl;
class CShap{
public:
virtual double Area() = 0;
virtual void PrintInfo() = 0;
};
class CRectangle :public CShap{
public:
double width, high;
virtual double Area();
virtual void PrintInfo();
};
class CCricle :public CShap{
public:
double r;
virtual double Area();
virtual void PrintInfo();
};
class CTriangle :public CShap{
public:
double a, b, c;
virtual double Area();
virtual void PrintInfo();
};
#endif


/*Compare.h*/
#ifndef Compare_h
#define Compare_h
#include"CShap.h"
bool Compare(const void*s1, const void*s2);
#endif


然后运行到这里就出问题了。。。
...全文
173 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aha_程序猿 2015-06-04
  • 打赏
  • 举报
回复
引用 3 楼 mewiteor 的回复:
(*it)->PrintInfo();
Aha_程序猿 2015-06-04
  • 打赏
  • 举报
回复
引用 4 楼 mewiteor 的回复:
[quote=引用 2 楼 mewiteor 的回复:] Compare的参数类型好像应该都是const CShap* &
写错了,应该是
CShap* const &
[/quote]
mewiteor 2015-06-03
  • 打赏
  • 举报
回复
引用 2 楼 mewiteor 的回复:
Compare的参数类型好像应该都是const CShap* &
写错了,应该是
CShap* const &
mewiteor 2015-06-03
  • 打赏
  • 举报
回复
(*it)->PrintInfo();
mewiteor 2015-06-03
  • 打赏
  • 举报
回复
Compare的参数类型好像应该都是const CShap* &
Aha_程序猿 2015-06-03
  • 打赏
  • 举报
回复
还有就是最后应该怎么设置输出……我是想调用类定义中的PrintInfo()函数实现输出……所以我应该怎么写?之前想的是it->PrintInfo();可是无法通过编译……

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧