Errors in code

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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


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
//sphere.cpp//
#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
const auto 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, unsigned int lineLength);
void printDoubleHorizontalLine(unsigned int lineLength);

#endif


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
//formatting.cpp//
#include <iostream>				// for cin,cout
#include <iomanip>				// for formatting cout
#include <string>
#include "formatting.h"	

using namespace std;
//***********************************************************************************

//***********************************************************************
// function is what fills in the '.'
//***********************************************************************
void printDecimal(long inNum)
{
char currentFill = cout.fill('.');
long currentFlags = cout.flags();

cout << left << setw(20) << inNum;
cout.fill(currentFill);
cout.flags(currentFlags);
}

//***********************************************************************
// function puts the value in base 16 format
//***********************************************************************
void printHex(long inNum)
{
	char currentFill = cout.fill('.');
	long currentFlags = cout.flags();

	cout.setf(ios::hex, ios::basefield);
	cout.setf(ios::showbase);
	cout << left << setw(20) << inNum;
	cout.fill(currentFill);
	cout.flags(currentFlags);
}

//***********************************************************************
// function puts the value in base 2
//***********************************************************************
void printBinary(int inNum, int numDigits)
{
	for (int shiftAmt = numDigits = 0; shiftAmt >= -1; shiftAmt--)
	{
		cout << ((inNum >> shiftAmt) & 01);
	}
	char currentFill = cout.fill('.');

	cout << setw(20 - numDigits) << '.';
	cout.fill(currentFill);
}

//***********************************************************************
// function puts the value in Scientific Notation
//***********************************************************************
void printScientific(float inNum)
{
	long currentFlags = cout.flags();
	int oldPrecision = cout.precision();

	cout.precision(2);
	cout.setf(ios::scientific, ios::floatfield);
	cout << inNum;
	cout.flags(currentFlags);
	cout.precision(oldPrecision);
}

//***********************************************************************
// function to center text
//***********************************************************************
void centerLine(const string &text, unsigned int lineLength)

{
	cout << setw((text.length() + lineLength) / 2) << text << endl;
}

//***********************************************************************
// Function to print horizontal line
//***********************************************************************
void printDoubleHorizontalLine(unsigned int lineLength)

{
	char currentFill = cout.fill('=');

	cout << setw(lineLength) << '=' << endl;
	cout.fill(currentFill);
}


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
#include <iostream>
#include <iomanip>
#include <string>
#include "formatting.h"
#include "sphere.h"

using namespace std;

int main()
{
	double volume = 0;
	double circumference = 0;
	//***********************************************************************
	//                     Function Call to Center Text 
	//***********************************************************************
	centerLine("Data On Spheres CS215 Sec ", 80);
	centerLine("By,", 80);
	centerLine("George Costanza", 80);

	//***********************************************************************
	//                     Function Call to Print Doulbe Horizontal Line
	//***********************************************************************	
	printDoubleHorizontalLine(80);

	//***********************************************************************
	// Print values in decimal, binary, hex and scientific
	//***********************************************************************	
	for (int i = 20; i < 31; i++)
		sphere::SetRadius(i);
		printDecimal(i);
	printBinary(i);
	printHex(circumference);
	printScientific(volume);

	//***********************************************************************
	//                     Function Call to Print Doulbe Horizontal Line
	//***********************************************************************	
	printDoubleHorizontalLine(80);

	return 0;
}


The compiler is usually more verbose and tells the line numbers too.

You have a for-loop on line 28. Which statements does it run 11 times?

You seem to set the radius of a sphere. Which sphere?
Thank you for the comment, keskiverto

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.
1
2
3
4
5
float sphere::CalcVolume()
{
	double volume = 0;
	volume = radius*radius*radius*PI * 4 / 3
};




Edit:

You've defined currentFlags as a long int. That doesn't appear to be a valid input into the flags member function. http://www.cplusplus.com/reference/ios/ios_base/flags/

1
2
3
long currentFlags = cout.flags();
/// 
cout.flags(currentFlags);
Last edited on
wildblue

Thank you for the comment.
I just fixed #2 &#3 as following and
trying to figure out #1 & currentflag right now.

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
#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
const auto 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

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
//sphere.cpp
#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
const auto 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;
};




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
//lab2.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "formatting.h"
#include "sphere.h"

using namespace std;

int main()
{
	double volume = 0;
	double circumference = 0;
	//***********************************************************************
	//                     Function Call to Center Text 
	//***********************************************************************
	centerLine("Data On Spheres CS215 Sec ", 80);
	centerLine("By,", 80);
	centerLine("George Costanza", 80);

	//***********************************************************************
	//                     Function Call to Print Doulbe Horizontal Line
	//***********************************************************************	
	printDoubleHorizontalLine(80);

	//***********************************************************************
	// Print values in decimal, binary, hex and scientific
	//***********************************************************************	
	for (int i = 20; i < 31; i++)
		sphere::SetRadius(i);
		printDecimal(i);
	printBinary(i);
	printHex(circumference);
	printScientific(volume);

	//***********************************************************************
	//                     Function Call to Print Doulbe Horizontal Line
	//***********************************************************************	
	printDoubleHorizontalLine(80);

	return 0;
}



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().
Last edited on
Topic archived. No new replies allowed.