Hi,
I m having couple errors on my assignment and have been spending
few hours to fix folliwng errors from lab2.cpp.
Error 1 error C2352: 'sphere::SetRadius' :
Error 2-3 error C2065: 'i' : undeclared identifier
Warning 4-5 warning C4244: 'argument' : conversion from 'double' to 'long', possible loss of data
Error 6 IntelliSense: a nonstatic member reference must be relative to a specific object
Error 7 IntelliSense: identifier "i" is undefined
//sphere.h//
#ifndef CS215Lab2_sphere_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_sphere_h // declarations. Use the file name of the identifier
//***********************************************************************
// Class declaration for sphere
//***********************************************************************
class sphere {
// private data member
int radius;
public:
sphere();
sphere(int InRadius);
void(SetRadius(int NewRadius));
int GetRadius();
float CalcCircumference();
float CalcVolume();
};
#endif
//sphere.cpp//
#include "sphere.h"
#include <iostream>
usingnamespace std;
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
constauto PI = 3.14159265;
//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
int radius = 0;
}
//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
radius = newRadius <= 0 ? 1 : newRadius;
} //to avoid the "0" issue and set a new radius
//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
for (int i = 20; i < 31; i++)
sphere::SetRadius(i);
}
//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
auto circumference = 0;
circumference = PI * 2(radius)
};
//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
double volume = 0;
volume = radius*radius*radius*PI * 4 / 3
};
1 2 3 4 5 6 7 8 9 10 11 12 13
//formatting.h//
#ifndef CS215Lab2_spherefunctions_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_spherefunctions_h // declarations. Use the file name of the identifier
void printDecimal(long inNum);
void printHex(long inNum);
void printBinary(int inNum, int numDigits = 8);
void printScientific(float inNum);
void centerLine(const std::string &text, unsignedint lineLength);
void printDoubleHorizontalLine(unsignedint lineLength);
#endif
For the output result, I need to print out the data in columns for spheres that have radii of (as a minimum) 20 to 30 in increments of 1 unit.
Sphere default is set in sphere.h.
In addition to what keskiverto alluded to in the for loop.... Not sure if this is just a cut and paste error, but it seems you meant to return the value of the radius here, not set it.
1 2 3 4 5
int sphere::GetRadius()
{
for (int i = 20; i < 31; i++)
sphere::SetRadius(i);
}
2(radius) looks like a function to the compiler and 2 wouldn't be a valid name. Do you mean PI * 2 * radius? And to return the value? Also, you can move the semicolon from the right side of the closing } to the end of the second line.
1 2 3 4 5
float sphere::CalcCircumference()
{
auto circumference = 0;
circumference = PI * 2(radius)
};
Same thing with the semicolon here and not returning a value.
#include "sphere.h"
#include <iostream>
usingnamespace std;
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
constauto PI = 3.14159265;
//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
int radius = 0;
}
//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
radius = newRadius <= 0 ? 1 : newRadius;
} //to avoid the "0" issue and set a new radius
//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
for (int i = 20; i < 31; i++)
sphere::SetRadius(i);
}
//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
auto circumference = 0;
circumference = PI * 2 * (radius);
};
//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
double volume = 0;
volume = PI * radius ^ 3 * 4/3;
};
I still get errors on following file for sphere.cpp and lab2.cpp
sphere.cpp
Warning 1 warning C4244: '=' : conversion from 'const double' to 'int', possible loss of data
Error 2 error C2296: '^' : illegal, left operand has type 'const double' Error 3 IntelliSense: expression must have integral or unscoped enum
Lab2.cpp
Error 3 IntelliSense: a nonstatic member reference must be relative to a specific object
Error 4 IntelliSense: identifier "i" is undefined
//sphere.cpp
#include "sphere.h"
#include <iostream>
usingnamespace std;
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
constauto PI = 3.14159265;
//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
int radius = 0;
}
//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
radius = newRadius <= 0 ? 1 : newRadius;
} //to avoid the "0" issue and set a new radius
//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
for (int i = 20; i < 31; i++)
sphere::SetRadius(i);
}
//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
auto circumference = 0;
circumference = PI * 2 * (radius);
};
//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
auto volume = 0;
volume = PI * radius ^ 3 * 4/3;
};
You probably misunderstood my question. Lets try again. Do you realize that this:
1 2 3 4 5 6
for (int i = 20; i < 31; i++)
sphere::SetRadius(i);
printDecimal(i);
printBinary(i);
printHex(circumference);
printScientific(volume);
means exactly same as this:
1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 20; i < 31; i++)
{
sphere::SetRadius(i);
}
printDecimal(i);
printBinary(i);
printHex(circumference);
printScientific(volume);
Second, you have essentially this:
1 2 3 4 5
#include "sphere.h"
int main() {
sphere::SetRadius(42);
return 0;
}
However, the class sphere does not have a static member function SetRadius. It does have a normal member SetRadius, but that can only be called with a sphere object. You have no sphere objects in main().