Need urgent help compile issue

Pages: 12
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;
^~~~~~~
Last edited on
Note you should be getting several warnings that give you valuable hints as to what is wrong, never ignore warnings.

The first thing I would recommend is that you comment out the code in main after your "cin" statement and just create one instance of one of your classes. For example "Rectangle test(rectHeight, rectWidth);" , get that one line to compile correctly before you try to complicate things with the vector and the unique_ptr.

With just that one instance here are the messages I get:

||=== Build: Debug in c++homework (compiler: GNU GCC Compiler) ===|
/main.cpp|10|warning: ‘class Shape’ has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|24|warning: base class ‘class Shape’ has accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|24|warning: ‘class Rectangle’ has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|33|warning: base class ‘class Shape’ has accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|33|warning: ‘class Triangle’ has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|42|warning: base class ‘class Shape’ has accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp|42|warning: ‘class Circle’ has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]|
/main.cpp||In function ‘int main()’:|
/main.cpp|58|error: no matching function for call to ‘Rectangle::Rectangle(int&, int&)’|
/main.cpp|24|note: candidate: ‘Rectangle::Rectangle()’|
/main.cpp|24|note: candidate expects 0 arguments, 2 provided|
/main.cpp|24|note: candidate: ‘Rectangle::Rectangle(const Rectangle&)’|
/main.cpp|24|note: candidate expects 1 argument, 2 provided|
/main.cpp|24|note: candidate: ‘Rectangle::Rectangle(Rectangle&&)’|
/main.cpp|24|note: candidate expects 1 argument, 2 provided|
/main.cpp|58|error: cannot declare variable ‘test’ to be of abstract type ‘Rectangle’|
/main.cpp|24|note: because the following virtual functions are pure within ‘Rectangle’:|
/main.cpp|21|note: ‘virtual double Shape::getarea() const’|
||=== Build failed: 2 error(s), 7 warning(s) (0 minute(s), 1 second(s)) ===|


Fixing those warnings is very important. You also seem to need several constructor and destructors.


If I comment below lines, I can see the below error. Can you provide some inputs here?

//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));


Below is the error.

main.cpp: In function ‘int main()’:
main.cpp:66:43: error: ‘shapes’ was not declared in this scope
const int totalArea = std::accumulate(shapes.begin(), shapes.end(), 0, [](int total, const auto& shape)
Minor: Lines 6-7 are unnecessary if you're going to bring in the entire std namespace at line 8. Suggestion: Change line 8 to using std::string;

Line 21 is a pure virtual function making Shape an abstract class. The compiler is telling you you can't instantiate a Rectangle because Shape is an abstract class. This is because there is no overload in Rectangle because there is no overload for line 21. Note that line 27 does not overload line 21 because of a difference in constness of the two functions.

Line 36: Ditto

Line 45: Ditto

Line 60: You're trying to construct a Rectangle with 2 arguments. Rectangle has no such constructor.

Line 61: Ditto

Line 62: Ditto

Line 68: totalArea is an int. getArea() return a double.




I have made the below changes, please check the code. I am not sure why again those errors are seen here? please help me.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>

using std::string;
constexpr double pi {3.1415926535897932384626643383279502884};

struct Pos
{
    int m_x;
    int m_y;
    Pos(const int x, const int y):m_x{x}, m_y{y} {}
};

class Point {
    private: int m_x;
             int m_y;
    public: Point(const Pos& posi): m_x{posi.m_x}, m_y{posi.m_y}{}
    Point() = default;
    int get_x_coord() const {return m_x;}
    int get_y_coord() const {return m_y;}
};

class Shape
{
  protected:
  double width, height;
  Point m_posit;
  Shape(const Point& posi): m_posit{posi} {}

  public:
    void set_data (double a, double b)
    {
        width = a;
        height = b;
    }
    virtual ~Shape() = default;
    virtual double getarea() const = 0;
};


class Rectangle: public Shape
{
private:
double m_width;
double m_height;

public:
    Rectangle(const Point& posi, const double width = 1.0, const double height = 1.0);
    double getarea() const override {return m_width * m_height;}
};

class Triangle: public Shape
{
public:
    double getarea ()
    {
        return (width * height)/2;
    }
};

class Circle : public Shape
{
  protected: 
    double m_radius;
    
  public:
    Circle(const Point& center, const double radius): Shape{center}, m_radius{radius} {}
    double getarea() const override {return pi* m_radius *m_radius;}
};


std::vector<Point> get_points();
std::vector<std::unique_ptr<Shape> get_shapes(const std::vector<Point>& points);

int main() {
    
    std::vector<Point> points{ get_points() };
    std::vector<std::unique_ptr<Shape> shapes {get_shapes(points)};
    int rectHeight = 0, rectWidth = 0;
    int triaHeight = 0, triaWidth = 0;
    int circRadius = 0;

    std::cin >> rectHeight >> rectWidth >> triaHeight >> triaWidth >> circRadius;
}

 std::vector<Point> get_points()
    {
        std::vector<Point> points
        {
            Point{Pos{0,0}}, Point{Pos{-100,-100}}, Point{Pos{300,300}}
        };
        return points;
    }
    
    std::vector<std::unique_ptr<Shape>> get_shapes(const std::vector<Point>& points)
    {
       std::vector<std::unique_ptr<Shape>> shapes 
       {
           std::make_unique<Rectangle>(rectHeight, rectWidth);
           std::make_unique<Triangle>(triaHeight, triaWidth);
           std::make_unique<Circle>(circRadius);
       };
       return shapes;
    }
    
    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;
}




Last edited on
Perhaps:

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
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>
//#include <string>

class Shape {
protected:
	//std::string name;
	double width {}, height {}, radius {};

public:
	Shape(double w, double h) : width(w), height(h) {}
	Shape(double r) : radius(r) {}
	virtual ~Shape() {}

	void set_data(double a, double b) {
		width = a;
		height = b;
	}

	virtual double getarea() const = 0;
};

class Rectangle : public Shape {
public:
	Rectangle(double w, double h) : Shape(w, h) {}

	double getarea() const override { return width * height; }
};

class Triangle : public Shape {
public:
	Triangle(double w, double h) : Shape(w, h) {}

	double getarea() const override { return (width * height) / 2; }
};

class Circle : public Shape {
public:
	Circle(double r) : Shape(r) {}

	double getarea() const override { return 3.1415 * (radius * radius); }
};

int main()
{
	double rectHeight {}, rectWidth {};
	double triaHeight {}, triaWidth {};
	double circRadius {};

	std::cout << "Enter rect height, rect width, tri height, tri width, circ rad: ";

	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 auto totalArea {std::accumulate(shapes.begin(), shapes.end(), 0.0, [](auto total, const auto& shape)
			{ return total + shape->getarea(); })};

	std::cout << totalArea << '\n';
}

Last edited on
Below are compiler errors seen:

main.cpp: In function ‘int main()’:
main.cpp:85:43: error: ‘get_points’ was not declared in this scope
std::vector<Point> points{ get_points() };
^
main.cpp:85:45: error: no matching function for call to ‘std::vector::vector()’
std::vector<Point> points{ get_points() };
^
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_vector.h:403:9: note: candidate: template std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)
vector(_InputIterator __first, _InputIterator __last,
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:403:9: note: template argument deduction/substitution failed:
/usr/include/c++/6/bits/stl_vector.h:375:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(initializer_list<value_type> __l,
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:375:7: note: conversion of argument 1 would be ill-formed:
/usr/include/c++/6/bits/stl_vector.h:350:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(vector&& __rv, const allocator_type& __m)
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:350:7: note: candidate expects 2 arguments, 1 provided
/usr/include/c++/6/bits/stl_vector.h:341:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(const vector& __x, const allocator_type& __a)
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:341:7: note: candidate expects 2 arguments, 1 provided
/usr/include/c++/6/bits/stl_vector.h:337:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = Point; _Alloc = std::allocator]
vector(vector&& __x) noexcept
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:337:7: note: conversion of argument 1 would be ill-formed:
/usr/include/c++/6/bits/stl_vector.h:320:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = Point; _Alloc = std::allocator]
vector(const vector& __x)
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:320:7: note: conversion of argument 1 would be ill-formed:
/usr/include/c++/6/bits/stl_vector.h:291:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = Point; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(size_type __n, const value_type& __value,
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:291:7: note: candidate expects 3 arguments, 1 provided
/usr/include/c++/6/bits/stl_vector.h:279:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(size_type __n, const allocator_type& __a = allocator_type())
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:279:7: note: conversion of argument 1 would be ill-formed:
/usr/include/c++/6/bits/stl_vector.h:266:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = Point; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::allocator_type = std::allocator]
vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:266:7: note: conversion of argument 1 would be ill-formed:
/usr/include/c++/6/bits/stl_vector.h:255:7: note: candidate: std::vector<_Tp, _Alloc>::vector() [with _Tp = Point; _Alloc = std::allocator]
vector()
^~~~~~
/usr/include/c++/6/bits/stl_vector.h:255:7: note: candidate expects 0 arguments, 1 provided
main.cpp:86:40: error: template argument 1 is invalid
std::vector<std::unique_ptr<Shape> shapes {get_shapes(points)};
^~~~~~
main.cpp:86:40: error: template argument 2 is invalid
main.cpp:86:65: error: ‘get_shapes’ was not declared in this scope
std::vector<std::unique_ptr<Shape> shapes {get_shapes(points)};
^
main.cpp:93:5: error: a function-definition is not allowed here before ‘{’ token
{
^
main.cpp:117:1: error: expected ‘}’ at end of input
}
See my previous post.
Hi seeplus

Your code compiles fine with no errors.
But while I try to provide input as below,
Input
4 3 5 2 5

Enter rect height, rect width, tri height, tri width, circ rad: 95.5375

Here for example, for values 4 and 3, area of rect must be 12 (since h = 4, w = 3 as per input given by user). Similarly for tri area must be 5 ( for h =5, w = 2). Also for circ area must be 79 (for rad = 5 here as input). So total area of shapes is 96, but as per compiler output I get 95.5375 only.
My code above works in double as per your original code - not int.
Is there any way to get 96 as total area instead if 95.5375? can i use round of function?
Last edited on
there are multiple ways, the easiest would be std::ceil in the <cmath> header.

cppreference has it documented well:
https://en.cppreference.com/w/cpp/numeric/math/ceil

the header itself is documented there as well:
https://en.cppreference.com/w/cpp/header/cmath
Do you mean to add this line?
std::cout << std::fixed
<< "ceil(totalArea) = " << std::ceil(totalArea) << '\n';

But I get compiler output as below. Here it gives 95.000000. For example, if it gives 95.5375, then I need to round of to nearest int, which is 96, instead of 95. i want to round a double to nearest int .How can i do this?
Enter rect height, rect width, tri height, tri width, circ rad: 4 3 5 2 5
ceil(totalArea) = 95.000000
Last edited on
std::ceil should print out 96.000000. So did it in my testrun of seeplus' code example.
if you want to round down, use floor
if you want to round up, use ceil
if you want to round to the nearest int, use round.
Ok, so for 95.5375, to truncate and get 96, should I use ceil? I am still not clear what you said here. if you tried with any sample code, feel free to share here.
Last edited on
Denver2020,

WHY do you want to round an area to the nearest integer? If you had a circle of radius 0.1 how big do you think that area would be?

If you are still hell-bent on doing it then JustShinigami has told you exactly which choices of function you need: is it so difficult to call those functions?

If it is purely for "pretty" output then simply set the precision to whatever you want when outputting.
The reason is im trying to run test and it fails, because not being integer value. As you can see the test expects output to be 96, but compiler output it as 95.5375. Please help me on this.


Input (stdin)

4 3 5 2 5
Your Output (stdout)
Enter rect height, rect width, tria height, tria width, circ rad: 95.5375

Expected Output
96

Last edited on
Take your pick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
   double x = 95.5375;
   cout << x << '\n';
   cout << round( x ) << '\n';
   cout << ceil( x ) << '\n';
   cout << (int)( x + 0.5 ) << '\n';
   cout << fixed << setprecision( 0 ) << x << '\n';
}


95.5375
96
96
96
96

Thanks for the quick tips.
After modifying the code, I got the below output, but test still fails.

Here in the code, created shape interface which has abstract function getarea() must have integer return value. But in the actual code its double. The integer values needed to calculate area for each of these shapes will be provided to constructor of each shape and then need to round result to nearest integer value before returning it. could this be the reason for test failure?


Input (stdin)

4 3 5 2 5
Your Output (stdout)
Enter rect height, rect width, tria height, tria width, circ rad: 96

Expected Output
96
Well if the expected output is
96

and your code produces
96

and the test says
Fail

then I should ask for your money back!
Pages: 12