Hello, I am hoping someone can tell me why I am getting this error in my code.
Code is for an assignment Instructions Below:
"Construct a class named Circle that has a floating-point data member named radius. The class should have a zero-argument constructor that initializes this data member to 0. It should have member functions named calcCircumference() and calCarea() that calculate the circumference and area of a circle respectively, a member function setRadius()to set the radius of the circle, a member function getRadius() to return the radius, and a member function showData() that displays the circle’s radius, circumference, and area. The formula for the area of a circle is A=πr2. The formula for the circumference of a circle is C=2πr.
The class should use appropriate protection levels for the member data and functions. It should also follow “principles of minimalization”: that is, no member data should be part of a class unless it is needed by most member functions of the object. A general rule of thumb is that “if you can easily calculate it, don’t store it.”
Use your class in a program that creates an instance of a Circle (utilizing the zero-argument constructor), prompts a user for a radius, calls the setRadius() function to set the circle’s radius, and then calls showData() to display the circle’s radius, circumference, and area. Your program should allow the user to enter circle dimensions until the user enters -1. Be sure to include appropriate error checking. Does it make sense to enter “abc” as the radius of a circle? No. Therefore, you should ensure that the user enters numeric data for the radius. Negative numbers (other than the -1 to exit) should also be prevented."
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
|
#include <iostream>
#include <limits>
#include "Circle.h"
#include "Circle.cpp"
using namespace std;
int main()
{
cout << "-Assignment 2" << endl;
double InputRadius;
while (true) {
cout << "Enter the Radious";
if (!(cin >> InputRadius));
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
if (InputRadius == -1.0)
{
break;
}
circle::setRadius(InputRadius);
circle::showData();
}
system("pause");
return 0;
}
|
The issue is Line 36 and 38. Both read an error E0245. (This is compiler Visual Studio 2019) error reads nonstatic member reference must be relative to specific object.
I have a header file of:
#pragma once
class circle
{
private:
double radius;
public:
circle();
circle(double rad);
double getRadius() const;
double calCircumference() const;
double calcArea() const;
void setRadius(double rad);
void showData() const;
};
And a functions .cpp file:
#include <iostream>
#include "Circle.h"
#define PI 3.14
circle::circle() : radius(0.0) {}
circle::circle(double rad)
{
if (rad < 0)
radius = 0.0;
else
radius = rad;
}
void circle::setRadius(double rad)
{
if (rad < 0)
radius = 0.0;
else
radius = rad;
}
double circle::getRadius() const
{
return radius;
}
double circle::calCircumference() const {
double circum = 2 * PI * getRadius();
return circum;
}
double circle::calcArea() const {
return (PI * getRadius() * getRadius());
}
void circle::showData() const {
std::cout << "Radius is: " << getRadius() << "Circumference is: " << calCircumference() << "Area is: " << calcArea() << std::endl;
}
Hoping someone can help!
Thanks