No declaration matches

I am trying to find the area and perimeter of a Circle using multiple files. I keep getting an error for no declaration matches. I have been working on this for over a week and cannot figure out what I am doing wrong.

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
Shape.h
#ifndef SHAPE_H
#define SHAPE_H


class Shape
{
    public:
        /** constructors */
        virtual float getArea()= 0;
        virtual float getPerimeter() = 0;

};

#endif // SHAPE_H


  circle.h

#ifndef Circle_H
#define Circle_H
#include "Shape.h"
const float PI = 3.14159265358979323846;

using namespace std;
                                   
class Circle: public Shape
{
 protected:
    float radius;                  

 public:                          
	Circle(float r)     
        {
        float getRadius();
        void setRadius(float radius);
       }          
};
#endif CIRCLE_H

Circle.cpp
#include "Circle.h"
#include "Shape.h"
#include <iostream>
using namespace std;


Circle::Circle(float r)
    {
    radius = r;
    }

    float Circle::getRadius()const
    {
    return radius;
    }

    void Circle::setRadius(float r)
    {
    radius = r;
    }

    float Circle::Shape()const
    {
    return PI * radius * radius;
    }

    float Circle::getPerimeter()const
    {
	return (2.0 * PI * radius);
    }

main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Triangle.h"

using namespace std;

int main()
{

float radius;
float getArea;
float getPerimeter;


cout << "----------------------------------------------" << endl << endl;
cout << "Calculations for a Circle" << endl << endl;
cout << "----------------------------------------------" << endl << endl;

cout << "Let's find the Area and Perimeter of a Circle:" << endl;
cout << endl;
        cout << "Enter the radius for the Circle: ";
        cin >> radius;
        cout << endl;
        //getArea = (PI * radius *radius);
        Circle circle;
        circle.getRadius();
        cout <<  "The area of the circle is: " << circle.getArea(); << "\n\n";
        cout << endl;
        cout <<  "AND"  << "\n\n"<< endl;
        cout <<  "The perimeter of the circle is: " << circle.getPerimeter(); << "\n\n";
cout << endl;
   return 0;
}


[/code]
Here is the code with all errors removed:
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
#ifndef SHAPE_H
#define SHAPE_H


class Shape
{
    public:
        /** constructors */
        virtual float getArea()= 0;
        virtual float getPerimeter() = 0;

};

#endif // SHAPE_H


#ifndef Circle_H
#define Circle_H
const float PI = 3.14159265358979323846;

using namespace std;
                                   
class Circle: public Shape
{
 protected:
    float radius;                  

 public:                          
	Circle(float r);
        float getRadius();
        void setRadius(float radius);

        virtual float getArea() override;
        virtual float getPerimeter() override;
};
#endif 

#include <iostream>
using namespace std;


Circle::Circle(float r)
    {
    radius = r;
    }

    float Circle::getRadius()
    {
    return radius;
    }

    void Circle::setRadius(float r)
    {
    radius = r;
    }

    float Circle::getArea()
    {
    return PI * radius * radius;
    }

    float Circle::getPerimeter()
    {
	return (2.0 * PI * radius);
    }

#include <iostream>

using namespace std;

int main()
{

float radius;
float getArea;
float getPerimeter;


cout << "----------------------------------------------" << endl << endl;
cout << "Calculations for a Circle" << endl << endl;
cout << "----------------------------------------------" << endl << endl;

cout << "Let's find the Area and Perimeter of a Circle:" << endl;
cout << endl;
        cout << "Enter the radius for the Circle: ";
        cin >> radius;
        cout << endl;
        //getArea = (PI * radius *radius);
        Circle circle{10};
        circle.getRadius();
        cout <<  "The area of the circle is: " << circle.getArea() << "\n\n";
        cout << endl;
        cout <<  "AND"  << "\n\n"<< endl;
        cout <<  "The perimeter of the circle is: " << circle.getPerimeter() << "\n\n";
cout << endl;
   return 0;
}
Note that the definition of a function must match the prototype. I.e. this

float Circle::getRadius()const
->
1
2
3
4
5
6
7
8
class Circle: public Shape
{
 protected:
    float radius;                  

 public:                          
	Circle(float r);
        float getRadius() const; // Note: const 
I made the suggested corrections and added an exception and now I get the following error:
main.cpp:50:51: error: 'cir' was not declared in this scope
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

    cout << "----------------------------------------------" << endl << endl;
    cout << "Calculations for a Circle" << endl << endl;
    cout << "----------------------------------------------" << endl << endl;

    cout << "Let's find the Area and Perimeter of a Circle:" << endl;
    cout << endl;
    cout << "Enter the radius for the Circle: ";
        cin >> radius;
    cout << endl;
    try{
        if(radius ==0)
        {
         throw 99;
        }
        Circle cir{10};
        cir.getRadius();
    }
    catch(float x)
    {
         cout <<"EXCEPTION: You cannot use 0 as your radius";
    }
        cout <<  "The area of the circle is: " << cir.getArea() << "\n\n";
        cout << endl;
        cout <<  "AND"  << "\n\n"<< endl;
        cout <<  "The perimeter of the circle is: " << cir.getPerimeter() << "\n\n";
        cout << endl;
Last edited on
could you post the full code?
Circle cir is declared within the scope of the try block. It is not visible to the logic in your catch block or outer code.

Are you required to use exceptions here for academic purposes? If not, I would just change your logic into an if/else chain.

1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> radius;
if (radius == 0)
{
    cout << "You cannot use 0 as your radius\n";
}
else
{
        Circle cir{10};
        cout <<  "The area of the circle is: " << cir.getArea() << "\n\n";
        cout << endl;
        cout <<  "AND"  << "\n\n"<< endl;
        cout <<  "The perimeter of the circle is: " << cir.getPerimeter() << "\n\n\n";
}

Alternatively, you could have the constructor of Circle throw.
Last edited on
Yes. I am required to add two custom exceptions which inheirt from std:exception.
Topic archived. No new replies allowed.