I got below errors while run the code. any ideawhat are these errors?
I use c++14.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>
using std::cout;
using std::unique_ptr;
using namespace std;
class Shape
{
public:
string name;
double width, height, radius;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
virtual double getarea() const = 0;
};
class Rectangle: public Shape
{
public:
double getarea ()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
double getarea ()
{
return (width * height)/2;
}
};
class Circle : public Shape
{
public:
double getarea ()
{
return 3.1415 * (radius * radius);
}
};
int main() {
int rectHeight = 0, rectWidth = 0;
int triaHeight = 0, triaWidth = 0;
int circRadius = 0;
std::cin >> rectHeight >> rectWidth >> triaHeight >> triaWidth >> circRadius;
std::vector<std::unique_ptr<Shape>> shapes;
shapes.emplace_back(std::make_unique<Rectangle>(rectHeight, rectWidth));
shapes.emplace_back(std::make_unique<Triangle>(triaHeight, triaWidth));
shapes.emplace_back(std::make_unique<Circle>(circRadius));
const int totalArea = std::accumulate(shapes.begin(), shapes.end(), 0, [](int total, const auto& shape)
{ return total + shape->getarea(); });
std::cout << totalArea << "\n";
return 0;
}
| |
Below are the errors:
Compilation failed due to following error(s).In file included from /usr/include/c++/6/memory:81:0,
from main.cpp:3:
/usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = Rectangle; _Args = {int&, int&}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Rectangle, std::default_delete<Rectangle> >]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',60)">main.cpp:60:74</span>: required from here
/usr/include/c++/6/bits/unique_ptr.h:795:30: error: invalid new-expression of abstract class type ‘Rectangle’
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:24:7: note: because the following virtual functions are pure within ‘Rectangle’:
class Rectangle: public Shape
^~~~~~~~~
main.cpp:21:20: note: virtual double Shape::getarea() const
virtual double getarea() const = 0;
^~~~~~~
In file included from /usr/include/c++/6/memory:81:0,
from main.cpp:3:
/usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = Triangle; _Args = {int&, int&}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Triangle, std::default_delete<Triangle> >]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',61)">main.cpp:61:73</span>: required from here
/usr/include/c++/6/bits/unique_ptr.h:795:30: error: invalid new-expression of abstract class type ‘Triangle’
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:33:7: note: because the following virtual functions are pure within ‘Triangle’:
class Triangle: public Shape
^~~~~~~~
main.cpp:21:20: note: virtual double Shape::getarea() const
virtual double getarea() const = 0;
^~~~~~~
In file included from /usr/include/c++/6/memory:81:0,
from main.cpp:3:
/usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = Circle; _Args = {int&}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Circle, std::default_delete<Circle> >]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',62)">main.cpp:62:60</span>: required from here
/usr/include/c++/6/bits/unique_ptr.h:795:30: error: invalid new-expression of abstract class type ‘Circle’
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:42:7: note: because the following virtual functions are pure within ‘Circle’:
class Circle : public Shape
^~~~~~
main.cpp:21:20: note: virtual double Shape::getarea() const
virtual double getarea() const = 0;
^~~~~~~