c++ - 如何使用参数为向量的方法?
问题描述
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class Shape{
protected:
int _r;
int _w;
int _h;
public:
Shape(double r) : _r(r) {}
Shape(double w, double h) : _w(w), _h(h) {}
virtual double area(vector<Shape *>){
cout << "shape:: area " << endl;
return _r;
}
};
class Circle : public Shape{
public:
Circle(double r) : Shape(r) {}
double area(vector<Shape *>) { return _r*_r*atan(1)*4.0; }
};
class Triangle : public Shape{
public:
Triangle(double s) : Shape(s) {}
double area(vector<Shape *>) { return sqrt(3) * pow(_r, 2) / 4; }
};
class Rectangular : public Shape{
public:
Rectangular(double w, double h) :Shape(w, h) {}
double area(vector<Shape *>) { return _w * _h ;}
};
int main()
{
int n;
char info;
int value;
int value2;
double sum;
vector<Shape > collection;
vector<int> answer;
sum = 0;
cin >> n;
for(int i = 0 ; i < n; i++)
{
cin >> info;
if (info == 'C')
{
cin >> value;
Circle c(value);
collection.push_back(c);
}
else if (info == 'R')
{
cin >> value;
cin >> value2;
Rectangular r(value, value2);
collection.push_back(r);
}
else
{
cin >> value;
Triangle t(value);
collection.push_back(t);
}
}
for (int i = 0; i < n ; i++)
{
sum += collection[i].area(&collection[i]);
}
cout << sum << endl;
}
如您所见,我使用了一个抽象类Shape,以及三个具体类Circle、Rectangular、Triangle。
我想总结各种形状的区域。如
第一个输入表示我们必须计算多少个形状。C代表圆形,R代表矩形,T代表正三角形。
如何从 'std::__1::__vector_base<Shape, std::__1::allocator >::value_type' 解决这个不可行的转换
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class Shape{
protected:
int _r;
int _w;
int _h;
public:
Shape(double r) : _r(r) {}
Shape(double w, double h) : _w(w), _h(h) {}
virtual double area(vector<Shape *>) = 0;
};
class Circle : public Shape{
public:
Circle(double r) : Shape(r) {}
double area(vector<Shape *>) { return _r*_r*atan(1)*4.0; }
};
class Triangle : public Shape{
public:
Triangle(double s) : Shape(s) {}
double area(vector<Shape *>) { return sqrt(3) * pow(_r, 2) / 4; }
};
class Rectangle : public Shape{
public:
Rectangle(double w, double h) :Shape(w, h) {}
double area(vector<Shape *>) { return _w * _h ;}
};
int main()
{
int n;
char info;
int value;
int value2;
double sum;
vector<Shape*> collection;
vector<int> answer;
sum = 0;
cin >> n;
for(int i = 0 ; i < n; i++)
{
cin >> info;
if (info == 'C')
{
cin >> value;
Circle c(value);
collection.push_back(&c);
}
else if (info == 'R')
{
cin >> value;
cin >> value2;
Rectangle r(value, value2);
collection.push_back(&r);
}
else
{
cin >> value;
Triangle t(value);
collection.push_back(&t);
}
}
for (int i = 0; i < n ; i++)
{
sum += collection[i]->area(collection);
}
cout << fixed << setprecision(2) << sum << endl;
}
我改变并修复!
解决方案
多态性不适用于具体类!
通过声明vector<Shape> collection;
,您声明了 a vector
of 、Shape
not of或。您可能希望成为能够利用多态性的类型。Circle
Triangle
Rectangular
collection
vector<Shape*>
您的代码的另一个问题是您没有通过collection
type vector<Shape>
,但collection[i]
它只是 type Shape
。
这可能也可以解释您的错误,因为您的编译器很可能想要解析Shape
为. 这是不可能的,因此可能会导致您的编译器错误。vector<Shape*>
area
此外,如果您想传递 just collection
,则必须确保类型匹配。vector<Shape>
不能隐式转换为vector<Shape*>
推荐阅读
- node.js - 如何在不使用 mongoose 和节点 js 在 mongodb 中插入不需要的字段数据的情况下创建文档?
- javascript - 图像地图为圆形区域设置背景图像
- .net - 如何使用 OrchardCore CMS 通过 GraphQL 从 MediaFields 获取所有内容?
- python - 通过传递坐标在棋盘上划出白色方块
- asp.net-core - 动态创建和销毁 Blazzor 客户端组件?
- ios - NSRangeException 由 String.subscript.getter
- python - 如何在python中的for循环函数的最后删除空格/跳转到下一行
- c# - 使用 Web API 在不写入磁盘的情况下读取 HTTP 文件和表单数据
- macos-catalina - 由于 OS Catalina,FICO Xpress Workbench 无法工作,产生错误“spawn mosel ENOENT”
- css - CSS变换过渡问题