error: expected a ';', bug in Intellisense when building? - c++

I got an error in Visual Studio 2019, when building and using IntelliSense at the same time, which said that it expected a ';' (CODE: E0065 and the error is supposed to be on line 9), I am extremely confused because this is my first time getting an error like this when defining functions because it is also my first time defining functions in c++, I really don't know where I could be missing a semi colon. The program doesn't run.
#include <iostream>
using namespace std;
int main() {
const double pi{3.14159};
double calc_area_circle(double radius) {
return pi * (radius * radius);
}
void area_circle() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
}
return 0;
}
If any one could clear this up for me, I would really appreciate it.

Nesting of function definition (defining functions inside another functions) is not allowed in C++ unless you use lambda functions.
Put the function definitions outside the function
#include <iostream>
using namespace std;
const double pi{3.14159};
double calc_area_circle(double radius) {
return pi * (radius * radius);
}
void area_circle() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
}
int main() {
return 0;
}
or convert them to lambda function
#include <iostream>
using namespace std;
int main() {
const double pi{3.14159};
const auto calc_area_circle = [&](double radius) -> double {
return pi * (radius * radius);
};
const auto area_circle = [&]() {
double radius{};
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
};
return 0;
}
Note: these programs will do nothing because the defined functions are not called.

Related

Displaying results of methods on the screen

My task is to practice inheritance, putting all the classes in separate files. I have a base class Circle and a derived class Cylinder.
What I'm stuck on is trying to display on the screen the result of my calculated area and volume for an object B of a Cylinder type. I found a way to do that for a Circle, though it doesn't work for my Cylinder.
circle.h
#pragma once
#include <iostream>
class Circle {
public:
float r;
Circle();
Circle(float r);
Circle circumference();
Circle area();
void view();
void getArea();
void getCircum();
};
circle.cpp
#include "circle.h"
#include <cmath>
using namespace std;
Circle::Circle() : r(5) {
cout << "Default constructor has been called for a circle\n";
}
Circle::Circle(float r) {
this->r = r;
cout << "Constructor with parameters has been called for a circle\n";
}
void Circle::view() {
cout << "Radius = " << r << endl;
}
Circle Circle::circumference() {
return 2 * M_PI * r;
}
Circle Circle::area() {
return M_PI * pow(r, 2);
}
void Circle::getArea() {
cout << "Area = " << r << " m^2";
}
void Circle::getCircum() {
cout << "Circumference = " << r << " m";
}
cylinder.h
#pragma once
#include <iostream>
#include "circle.h"
class Cylinder : public Circle {
public:
float h;
Cylinder();
Cylinder(float r, float h);
void view();
double area();
double volume(float r, float h);
void getArea();
void getVolume();
};
cylinder.cpp
#include "cylinder.h"
#include <cmath>
using namespace std;
Cylinder::Cylinder() : h(7) {
cout << "Default constructor has been called for a cylinder\n";
}
Cylinder::Cylinder(float r, float h) : Circle(r) {
this->h = h;
cout << "Constructor with parameters has been called fo a cylinder\n";
}
void Cylinder::view() {
Circle::view();
cout << "Height = " << h << endl;
}
double Cylinder::area() {
return 2 * M_PI * r * h;
}
double Cylinder::volume(float r, float h) {
return M_PI * pow(r, 2) * h;
}
void Cylinder::getArea() {
cout << "Area = " << h;
}
void Cylinder::getVolume() {
cout << "Volume = " << h;
}
main.cpp
#include <iostream>
#include "circle.h"
#include "cylinder.h"
using namespace std;
int main() {
Circle A;
A.view();
Circle A1(8);
A1.view();
Cylinder B;
B.view();
Cylinder B1(4, 6);
B1.view();
//A.area().getArea();
//cout << endl;
//A.circumference().getCircum();
//cout << endl;
//A1.area().getArea();
//cout << endl;
//A1.circumference().getCircum();
B.area().getArea();
return 0;
}
The error that I'm getting:
main.cpp: In function ‘int main()’:
main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’
26 | B.area().getArea();
| ^~~~~~~
I feel like neither my code in main() for instance B, nor my methods getArea() and getVolume() in class Cylinder, are correct. And there is probably a better approach to do the same for an object A and A1 of a Circle type, though the code I commented out actually works.
I know that this is a dumb question, and such things should be quite straightforward, but I am trying to learn and would be grateful for any advice on how I can fix this.
Well, the reason you are getting the error message:
main.cpp: In function ‘int main()’:
main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’
26 | B.area().getArea();
| ^~~~~~~
Is because you are basically doing this:
auto _ = B.area();
So here, _ deduces to be a double, and then you do:
_.getArea();
You are trying to access a member function from a double, and double doesn't have any member functions.
You probably meant to do this instead:
auto x = B.area();
B.h = x;
B.GetArea();
This assigns the area of B.area() to a variable x, and assigns x to B.h. B's member function then gets called and outputs the area.
In your getArea() function, instead of saying:
cout << "Area = " << endl;
Just say:
cout << "Area = " << area() << endl;
Then in your main.cpp, just call B.getArea().
Hope this helps!

VS2022 code analysis error using uninitialized memory C++ [duplicate]

This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Calling function with variable that is being initialized [duplicate]
(1 answer)
Closed 5 months ago.
Here are the errors on the following code after the VS2022(v143) upgrade:
Could someone please suggest what is wrong here and how to fix it?
//Cricle properties problem
#include <iostream>
using namespace std;
float Qradius(float diameter)
{
float radius = diameter / 2;
return radius;
}
float Warea(float radius)
{
float area = (radius *radius) *3.14;
return area;
}
float Ecircumference(float diameter)
{
float circumference = 3.14 * diameter;
return circumference;
}
float Rarclength(float arcangle, float circumference)
{
float arclength = (circumference *arcangle) / 360;
return arclength;
}
int main()
{
float diameter, arcangle;
float area, circumference, arclength, radius;
cout << "Type the diameter ";
cin >> diameter;
cout << "Type the arcangle ";
cin >> arcangle;
cout << "The radius of the circle is " << Qradius(diameter) << endl;
cout << "The area is " << Warea(radius) << endl;
cout << "The circumference is " << Ecircumference(diameter) << endl;
cout << "The arc length is " << Rarclength(arcangle, circumference) << endl;
}
I solved the warnings and explained why they were coming up in the comments in the code. Also do not use using namespace std;
#include <iostream>
float Qradius(float diameter)
{
float radius = diameter / 2;
return radius;
}
float Warea(float radius)
{
// if no f is specified, the compiler assumes it is a double
// the warning tells you that it converts a double to float
// which could lead to loss of data (C4244)
float area = (radius *radius) * 3.14f;
return area;
}
float Ecircumference(float diameter)
{
// same as aboth
float circumference = 3.14f * diameter;
return circumference;
}
float Rarclength(float arcangle, float circumference)
{
float arclength = (circumference *arcangle) / 360;
return arclength;
}
int main()
{
float diameter, arcangle;
// area and arclength are unused (C4101)
float /*area,*/ circumference, /*arclength,*/ radius;
std::cout << "Type the diameter ";
std::cin >> diameter;
std::cout << "Type the arcangle ";
std::cin >> arcangle;
// radius and circumference is never set
// and later used without setting any value (C6001)
radius = Qradius(diameter);
circumference = Ecircumference(diameter);
std::cout << "The radius of the circle is " << radius << std::endl;
std::cout << "The area is " << Warea(radius) << std::endl;
std::cout << "The circumference is " << circumference << std::endl;
std::cout << "The arc length is " << Rarclength(arcangle, circumference) << std::endl;
}

C2597 illegal reference to non static member

Trying to get this homework figured out and I just keep hitting one wall after another. What I am getting now is the error message:
Error 1 error C2597: illegal reference to non-static member 'circleType::radius'
I have 2 header files, circleType.h and cylinderType.h and I need to out put results for shipping and painting costs that a user would enter. A little help before I go completely out of my mind ... Thank you.
circle.h
class circleType
{
public:
static void setRadius(double r);
double getRadius();
double area();
double circumference();
circleType(double r = 0);
private:
double radius;
};
void circleType::setRadius(double r)
{
if (r >= 0)
{
radius = r;
}
else
{
radius = 0;
}
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
}
cylinderTyper.h
#include "circleType.h"
class cylinderType: public circleType
{
public:
static void setRadius(double r);
static double getRadius();
static double area();
static double circumference();
cylinderType(double r = 0);
private:
double radius;
};
main.cpp
#include "stdafx.h"
#include "cylinderType.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void enterData(int& cylinderBase,int& cylinerHeight, double& shipCost, double& paintCost);
int main()
{
cout << fixed << showpoint << setprecision(2);
int cylinderBase, cylinderHeight;
double sCost, pCost, shipCost, paintCost, volume, area = 0, circumference = 0;
enterData(cylinderBase, cylinderHeight, shipCost, paintCost);
cylinderType::setRadius(cylinderBase + cylinderHeight);
cylinderType::getRadius();
cylinderType::area();
cylinderType::circumference();
cout << "Cost of shipping: $" << circumference * shipCost << endl;
cout << "Cost of painting: $" << area * paintCost << endl;
system("pause");
return 0;
}
void enterData(int& cylinderBase, int& cylinderHeight, double& shipCost, double& paintCost)
{
cout << "Enter the base size of cylinder: ";
cin >> cylinderBase;
cout << endl;
cout << "Enter the hight size of cylinder: ";
cin >> cylinderHeight;
cout << endl;
cout << "Enter shipping cost per liter: ";
cin >> shipCost;
cout << endl;
cout << "Enter cost of painting per square foot: ";
cin >> paintCost;
cout << endl;
}
It's a very simple rule: static member functions can only access member variables that are static as well. That's because a static function isn't called against a specific object, so object members don't make sense in that context.
In your case, the static function setRadius is trying to modify the member variable radius which is not static. I suspect that you really don't want setRadius to be a static function.

Having trouble outputting using class

I am having trouble with the output part of the problem, I am getting errors on the lines that say bottom right, top left, and dimension. What am i doing wrong?
I have tried many things and I just do not know how to get it to work correctly, and we have not gone over anything like this kind of output in class:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
double px;
double py;
public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};
class Triangle
{
private:
Point blPoint;
double length, height;
public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);
Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;
double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);
int main()
{
// Define local variables
Triangle tri;
double sx, sy;
//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);
// Display triangle information
tri.display();
// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;
cout << "Enter scale factor in y direction: ";
cin >> sy;
// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);
// Display triangle information
tri.display();
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:
void Point::setX(const double x)
{
px = x;
}
void Point::setY(const double y)
{
py = y;
}
double Point::getX() const
{
return (px);
}
double Point::getY() const
{
return (py);
}
void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.setX(x);
}
void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}
void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}
void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}
Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint);
}
Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
Point getBottomRight;
double mx = (blPoint.getX()+ length);
getBottomRight.setX(mx);
return(getBottomRight);
}
Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}
double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}
double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}
double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
//hypotenuse = (sqrt((height * height)+(length * length)));
return (sqrt((height * height)+(length * length)));
}
double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
//perimeter = ((sqrt((height * height)+(length * length)))+ height + length);
return ((sqrt((height * height)+(length * length)))+ height + length);
}
void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
length = scalefact * length;
}
void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}
void Triangle::display() const
{
/* INSERT YOUR CODE */
cout <<"---------------------------------------" << endl;
cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ')' <<endl;
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft.getY() << ')' << endl;
cout << "Bottom Right Vertex (" << getBottomRight.getX() << ", " << blPoint.getY() << ')' << endl;
cout << "Dimensions (" << getBottomRight.getX()- blPoint.getX() << ", " << getTopleft.getY() - blPoint.getY() << ')' << endl;
cout << "Hypotenuse = " << hypotenuse() << endl;
cout << "Perimeter = " << perimeter() << endl;
cout <<"---------------------------------------" << endl;
}
double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);
cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);
cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);
cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}
You are using the functions like they are variables you need to add () to call them correctly:
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft().getY() << ')' << endl;
^^
cout << "Bottom Right Vertex (" << getBottomRight().getX() << ", " << blPoint.getY() << ')' << endl;
^^
cout << "Dimensions (" << getBottomRight().getX()- blPoint.getX() << ", " << getTopLeft().getY() - blPoint.getY() << ')' << endl;
^^ ^^
Also, read_triangle does not have a return statement but you declare that it returns double. Flowing off the end of a value returning function is undefined behavior and therefore you can not rely on the results. It does not look like you are using the results so you may want to just change the function to return void and that will fix it.

"class::data member is private" error, but I'm operating on it with a member function?

I'm fairly new to C++, and I don't understand what is triggering this error:
/home/---/Documents/C++/---_lab2/lab2c.cpp||In function ‘int main()’:|
Line 9: error: ‘float circle::x1’ is private
Line 58: error: within this context
I know the data member x1 (x2,y1,y2 as well) is private, but I am operating on the object myObj using functions that are members of the class circle, so shouldn't they still work? Can someone explain to me what's wrong here?
#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
class circle{
private:
float x1,y1,x2,y2;
protected:
float distance(float x1,float y1,float x2, float y2){
return sqrt(fabs((x2-x1)*(x2-x1))+fabs((y2-y1)*(y2-y1)));
};
public:
float radius(float x1, float y1, float x2, float y2){
float rad = distance(x1,y1,x2,y2);
return rad;
};
float circumference(float rad){
return 2*PI*rad;
};
float area(float rad){
return PI*rad*rad;
};
float populate_classobj(float x1main,float x2main,float y1main,float y2main){
x1 = x1main;
x2 = x2main;
y1 = y1main;
y2 = y2main;
};
};
int main(){
circle myObj;
float x1main,x2main,y1main,y2main;
cout << "Coordinates of center" << endl;
cout << "X: ";
cin >> x1main;
cout << "Y: ";
cin >> y1main;
cout << "Coordinates of point on circle" << endl;
cout << "X: ";
cin >> x2main;
cout << "Y: ";
cin >> y2main;
myObj.populate_classobj(x1main,x2main,y1main,y2main);
cout << "Radius is " << myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2) << endl;
cout << "Circumference is " << myObj.circumference(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;;
cout << "Area is " << myObj.area(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;
return 0;
}
You're attempting to access private members outside the class when you call radius & other methods.
But your real problem is with the logic. Why do you need to pass parameters to, for example, the radius method of your class:
float radius(float x1, float y1, float x2, float y2){
float rad = distance(x1,y1,x2,y2);
return rad;
};
The circle is already self-contained, why not just:
float radius(){
float rad = distance(x1,y1,x2,y2);
return rad;
};
Same with:
float circumference(){
return 2*PI*radius();
};
float area(){
return PI*radius()*radius();
};
Also, note that:
circle myObj;
creates an invalid object. You shouldn't have to call populate_classobj just to make it valid. Instead, have a proper constructor:
circle(float x1main,float x2main,float y1main,float y2main) :
x1(x1main),
x2(x2main),
y1(y1main),
y2(y2main)
{
};
and create the object as:
circle myObj(x1main,x2main,y1main,y2main);
The various insertion statements at the end of main try to use myObj.x1, which tries to use the member x1 of myObj. They can't, because x1 is private. It doesn't matter what the code is doing with that value; private is private. You can access the value from inside a member function or a friend function, but not from outside.
cout << "Radius is " << myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2) << endl;
cout << "Circumference is " << myObj.circumference(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;;
cout << "Area is " << myObj.area(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;
You can't access a private variable. Also you shouldn't have to do that.
Your method signature should be myObj.radius() or myObj.area() as x1 y1 x2 y2 are already members of the circle myObj. So passing them again as arguments is redundant.