Quick help for assignment due at 12 tonight! "Identifier Not Found"

Sorry for the demanding subject and short notice. I completely forgot I had a project due today at 12. I am very profficient at C# and this C++ has not been a problem for so far but I just started this and its maybe because of my time limit that im freaking out. Im getting two errors saying "identifier not found" when I am trying to call two overloaded functions. Its a simple program using classes functions and finding the surface area of a sphere. here is my code

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main ()
{
cout<<"Lab 10 has started for Adrian Campos"<<endl<<endl;
double dblRadius = 0;
double dblSurfaceArea = 0;
string strLoop = "";
do
{
do
{

cout<<"Please enter the radius of the sphere you wish to solve for! :";
cin>>dblRadius;
cout<<""<<endl;

if (dblRadius < 0 || dblRadius > 5000)
cout<<"Sorry your radius value must be greater than or equal to 0 but less than or equ-al to 5000"<<endl<<endl;


} while (dblRadius < 0 || dblRadius > 5000);



if (dblRadius == 0)
{
Sphere();
double surfaceArea();
}

else
{
Sphere(dblRadius);
double surfaceArea();
}
cout<<"Would you like to calculate another surface area? :";
cin>>strLoop;
cout<<""<<endl<<endl;
} while (strLoop == "y" || strLoop == "Y");

}

class Sphere
{
private: //Question 1:
double r, sArea; //is this declaring 2 instances of a double in one line? I thought that was bad practice..

public: //Question 2:
//This colon after public, what does it mean? I am guessing in C++ double colons indicate inheritance from
// a base class and in C# its a single colon so what does this single colon mean?


Sphere (); //Question 3:
Sphere (double radius); //this is an overloaded constructor correct?
double surfaceArea();
}; //Question 4:
//Why is there a semi-colon after the end brace? I've never used that in C# I thought curly
//braces replaced semi colons..

Sphere::Sphere()
{
r = 5;
}

Sphere::Sphere(double radius)
{
r = radius;
}

double Sphere::surfaceArea()
{
const double PI = 3.14159;
sArea = 4 * PI * r * r;
return sArea;
}


Alright I realize I have a bunch of errors I need to take time for this.
Question 1: Yes. And yes. Well, I would never declare variables in that way.

Question 2: The colon after private and public is just to end the statement. Here it has no other use or meaning.

The : only means base classes follow at the start of class declaration. In constructor definitions is means the start of the initializer list, which can include the constructors of base classes and member variables. And then you've got the labels in switch statements, and the triad operator (e.g. = (a < b) ? a : b), ...

Question 3: Sphere (double radius); is fine

But it is better to use the initializer list in its implementation, eg.

1
2
3
4
5
6
7
Sphere::Sphere() : r(5.0) // the .0 makes it clearet that it's a double
{
}

Sphere::Sphere(double radius) : r(radius)
{
}


is better

Question 4: because that's the way it is!(?) (Hopefully someone who has a better grasp of parser theory can explain by it's needed here, and similarly for struct and enum declarations).

Andy

P.S. And things have got to be declared before use!

If your file is pasted as-is, I can see the class Sphere is declared after you first try to use it in main();

The implementation (or definition) of class Sphere can be provided after main, but not its declaration.

P.P.S. Also, please edit your above ost to yses tags: "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Here is a quick solution to try:

Put the int main() at the bottom of the file. Sphere() needs to be declared before its first use.
Oh to answer question 4:

The semi-colon comes at the end of a class definition because you could also put additional arguments after that.
1
2
3
class MyClass {
...
} MyClassA;

This defines the MyClass class and declares MyClassA at the same time. Usually it isn't done, but it is possible.
I got you! The main problem was that I had my class below main. I know that when you have functions below your main you need to provide a sort of pointer(i forgot what they were called) to tell the compiler that there are functions after your main otherwise your program will not work. Does that also go for classes that are set up with in the same class file as your main? also one more question: some part of my logic is not working

do
{

cout<<"Please enter the radius of the sphere you wish to solve for! :";
cin>>dblRadius;
cout<<""<<endl;

if (dblRadius < 0 || dblRadius > 5000)
cout<<"Sorry your radius value must be greater than or equal to 0 but less than or equ-al to 5000"<<endl<<endl;


} while (dblRadius < 0 || dblRadius > 5000);


the if(dblradius <0 || dblRadius > 5000)
does not work for decimal values smaller than 0 but does work for negative values. If I put in .5 the program uses that as my input for radius and gives me the correct calculation; however, with my logic it should tell the user to enter a bigger value and loop until a value withing the correct range is inputted..
Those pointer things that go ahead of your main are called declarations or function headers. They apply to everything from functions to global variables to classes and structs.

If you wanted to keep the class at the bottom, then you need to put a header at the top. It would look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Sphere
{
private:
	double r, sArea;
public:	 
	Sphere ();	
	Sphere (double radius);
	double surfaceArea(); 
};

int main()
{
... //The rest of your stuff down here
}


The constructor and surfaceArea() function that are a part of class Sphere are declared in this header as well so you don't have to do those again.

As per your second comment...
0.5 is larger than 0. There are no decimal values smaller than 0. I assume that you are trying to check if something is less than 1. What the function is returning is correct.
Last edited on
thank you stewbond. That was a brainfart on my part. It actually is working correct I just spaced for some reason I was thinking a decimal is smaller than 0. Thanks to everyone who helped me out!
Topic archived. No new replies allowed.