I thought I was following a recipe that worked, but not so. Can someone explain to me why this does not compile? (And I have yet to include a couple of constructors!) - Thanks, Rich.

//CIS 180 Rich Mansfield 0457321 12/6/09
//Lab11.1  -  This program illustrates the use of classes
#include <iostream>
using namespace std;

class Circle
{
   	private:
      		double radius;
      		const double PI = 3.1416;
   	public:
 		void   setRadius(double);
     
		double getRadius() const;
    		double getDiameter() const;
     		double getCircumference() const;
     		double getArea() const;
};    

void Circle::setRadius(double r)
{ 
   	radius = r; 
}

double Circle::getRadius() const
{ 
	return radius; 
}

double Circle::getDiameter() const
{ 
	return 2 * radius; 
}

double Circle::getCircumference() const
{ 
	return 2 * PI * radius; 
}

double Circle::getArea() const
{ 
	return PI * radius * radius; 
}

int main()
{
   	Circle C1;
	double radiusLength;

	cout << “Input the radius:” ;
	cin >> radiusLength;
		
	C1.setRadius(radiusLength);
   
	cout << "Here is the circle C1’s data:\n";
   	cout << "Radius: " << C1.getRadius() << endl;
	cout << "Diameter: " << C1.getDiameter() << endl;
	cout << "Circumference: " << C1.getCircumference() << endl;
	cout << "Area: " << C1.getArea() << endl;

	return 0;
}

/* These error msgs I'm getting make no sense to me:
  ----jGRASP exec: c++ -g /Users/richmansfield/lab11.2
    ld warning: in /Users/richmansfield/lab11.2, file is not of required architecture
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    
     ----jGRASP wedge: exit code for process is 1.
     ----jGRASP: operation complete. */

Recommended Answers

All 21 Replies

Well, I took out the PI constant, and everything worked. That might be your problem. Also, as the poster below me said, you have curly quotations on one of your lines.

I don't know if this first part has anything to do with it, but were you writing your strings in another program, because the quotation marks on line 50 and the apostrophe on line 55 are the "curly" ones used by word processors and are interpreted differently by the compiler (as ASCII 147 and 148 for the quotes)

It's more likely a problem with your PI constant. Constants in classes need to be static http://www2.research.att.com/~bs/bs_faq2.html#in-class or an enum. So make it static const double PI = 3.1416;

Thanks - I should have thought of that, as that has occurred before. But correcting it did not solve the problem. I'm going to try the other suggestion next, about putting in a constructor (although there was no constructor in the recipe I was following!).

Hi again, and thanks for the fast replies. I tried changing the curly quotes, which probably helped but not enough, and tried introducing a constructor - which didn't work either, though that could be a syntax error on my part. I'm going to try the static idea for PI next.

//CIS 180 Rich Mansfield 0457321 12/6/09
//Lab11.1  -  This program illustrates the use of classes

#include <iostream>
using namespace std;


class Circle
{
   
	private:
      
		double radius;
      
		const double PI = 3.1416;
   
	public:
		Circle();
		void   setRadius(double);
     
		double getRadius() const;
    
		double getDiameter() const;
     
		double getCircumference() const;
     
		double getArea() const;
};    

Circle::Circle();
{
	cout	<< "Hello, world!"\n\n";
}

void Circle::setRadius(double r)
{ 
   
	radius = r; 
}


double Circle::getRadius() const
{ 
	
	return radius; 
}

double Circle::getDiameter() const
{ 
	
	return 2 * radius; 

}


double Circle::getCircumference() const
{ 
	
	return 2 * PI * radius; 
}


double Circle::getArea() const
{ 
	
	return PI * radius * radius; 
}



int main()
{
   
	Circle C1;
	double radiusLength;

   

	cout << "Input the radius:" ;
      
	cin >> radiusLength;
		
	C1.setRadius(radiusLength);
   

	cout << "Here is the circle C1's data:\n";
   
	cout << "Radius: " << C1.getRadius() << endl;
   
	cout << "Diameter: " << C1.getDiameter() << endl;
   
	cout << "Circumference: " << C1.getCircumference() << endl;
   
	cout << "Area: " << C1.getArea() << endl;

   

	return 0;
}

/* The error msgs I'm getting make no sense to me:
     ----jGRASP exec: c++ -g /Users/richmansfield/lab11.2
    ld warning: in /Users/richmansfield/lab11.2, file is not of required architecture
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    
     ----jGRASP wedge: exit code for process is 1.
     ----jGRASP: operation complete.
     
and further, when 'run' was attempted
     ----jGRASP exec: /Users/richmansfield/a.out
     ----jGRASP wedge: could not execute  /Users/richmansfield/a.out
     ----   error number 2.
     ----   
     ----   Target does not exist or is not on PATH.
    
     ----jGRASP: operation complete.*/

 ----jGRASP exec: c++ -g /Users/richmansfield/lab11.2
ld warning: in /Users/richmansfield/lab11.2, file is not of required architecture
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

just for kicks try renaming your file to lab11_2.cpp see if that's what it's complaining about (after you try putting the static part in because that is important) -- it really shouldn't make any difference but it's highlighting the .2 in the filename in red.

You can't do this in the declaration:

const double PI = 3.1416;

Initialize PI in the constructor...

class Circle
{
private:
   double radius;
   const double PI;
public:
   Circle(double rad = 0) : radius(rad), PI(3.1416)
   {
   }
   // ...

Or make it static...

{
private:
   double radius;
   static const double PI;
public:
   void   setRadius(double);

   double getRadius() const;
   double getDiameter() const;
   double getCircumference() const;
   double getArea() const;
};

const double Circle::PI = 3.1416;
commented: Helpful for me too +1

just for kicks try renaming your file to lab11_2.cpp

Yup - That solved the worst problems with the incomprehensible error messages! The program runs okay now as it is; now I just have to work on getting user input in, and putting in a couple of constructors... Thanks.

Made a lot of progress by changing the filename and curly quotes and non-static constant. The last thing is to get constructors to work right. The default is supposed to initialize radius as 0; the other's supposed to pass user's input as an argument. I had the input part working until I put in the constructors, but now I'm getting all kinds of errors...

//CIS 180 Rich Mansfield 0457321 12/6/09
//Lab11.1  -  This program illustrates the use of classes

#include <iostream>
using namespace std;

class Circle
{
   	private:
      		double radius;
      		static const double PI;   
	public:
		Circle();
		Circle(double);

		void   setRadius(double);
     		double getRadius() const;
    		double getDiameter() const;
     		double getCircumference() const;
     		double getArea() const;
};    

Circle::Circle()//default constructor
{
	radius = 0.0;
}

Circle::Circle(double r)//arg-passing constructor
{
	radius = r;
}

const double Circle::PI = 3.1416;

void Circle::setRadius(double r)
{ 
   	radius = r; 
}

double Circle::getRadius() const
{ 
	return radius; 
}

double Circle::getDiameter() const
{ 
	return 2 * radius; 
}

double Circle::getCircumference() const
{ 
	return 2 * PI * radius; 
}

double Circle::getArea() const
{ 
	return PI * radius * radius; 
}

int main()
{
   	Circle C1();
	C1.setRadius();
   
	cout << "Here is the circle C1's data:\n";
   	cout << "Radius: " << C1.getRadius() << endl;
   	cout << "Diameter: " << C1.getDiameter() << endl;
   	cout << "Circumference: " << C1.getCircumference() << endl;
   	cout << "Area: " << C1.getArea() << endl;

	Circle C2(double r);
	double radiusLength;

	cout << "Input the radius:" ;
      	cin >> radiusLength;
		
	C2.setRadius(radiusLength);
   
	cout << "Here is the circle C2's data:\n";
   	cout << "Radius: " << C2.getRadius() << endl;
   	cout << "Diameter: " << C2.getDiameter() << endl;
   	cout << "Circumference: " << C2.getCircumference() << endl;
   	cout << "Area: " << C2.getArea() << endl;
	
	return 0;
}

Just a couple of small changes:

Circle C1; //no () after a default constructor call
	//C1.setRadius();  not sending anything in so there's no point
         //requires a double to be passed in so not overloaded for void
	cout << "Here is the circle C1's data:\n";
   	cout << "Radius: " << C1.getRadius() << endl;
   	cout << "Diameter: " << C1.getDiameter() << endl;
   	cout << "Circumference: " << C1.getCircumference() << endl;
   	cout << "Area: " << C1.getArea() << endl;

	Circle C2(0.0);  //were trying to send in "double r" doesn't make sense for a call  (could have set r = 0.0; and Circle C2(r); )
commented: This was lots of help; almost all done now. +1

Side notes...

With constructors, prefer an initialization list. And you can use default values with your constructor, so you could combine both constructors into this:

class Circle
{
private:
   double radius;
   static const double PI;
public:
   Circle(double radius_ = 0);

   void   setRadius(double);
   double getRadius() const;
   double getDiameter() const;
   double getCircumference() const;
   double getArea() const;
};

Circle::Circle(double radius_) : radius(radius_)
{
}

This one constructor is then both the default constructor and the "arg-passing constructor".

Thanks to a lot of help, I could make everything work by now except for something wrong in the displayCircle function.

//CIS 180 Rich Mansfield 0457321 12/7/09
//Lab11.1  -  This program illustrates the use of classes

#include <iostream>
using namespace std;

class Circle
{
   	private:
      		double radius;
      		static const double PI;   
	public:
		Circle();
		Circle(double);

		void   setRadius(double);
     		double getRadius() const;
    		double getDiameter() const;
     		double getCircumference() const;
     		double getArea() const;
                
		// This didn't work: void 	 displayCircle() const;
};    

const double Circle::PI = 3.1416;

Circle::Circle()//default constructor, inited radius
//Constructors set aside memory but don't name objs
{
	radius = 0.0;
}

Circle::Circle(double r)//arg-passing constructor
//radius inited to r above
{
	radius = r;
}

void Circle::setRadius(double r)
{ 
   	radius = r; 
}

double Circle::getRadius() const
{ 	
	return radius; 
}

double Circle::getDiameter() const
{ 
	return 2 * radius; 
}

double Circle::getCircumference() const
{ 	
	return 2 * PI * radius; 
}

double Circle::getArea() const
{ 	
	return PI * radius * radius; 
}

//This is the only non-working part: void Circle::displayCircle() const
{ 	
	cout	<< "The radius of C1 is "	<< radius				<< endl
		<< ", and its area is "	<< PI * radius * radius	<< endl	<< endl; 
}

int main()
{
   	cout << "1. Create a circle object C1 with radius of zero:\n";
   	Circle C1;//One req was to have a default constructor set to zero
	cout << "Here is C1's original data:\n";
   	cout << "Radius: " << C1.getRadius() << endl << endl;
   
	cout << "2. Create a circle object C2 with radius of 5.0:\n";
   	Circle C2(5.0);
	cout << "Here is C2's original data:\n";
	cout << "Radius: " << C2.getRadius() << endl << endl;
   
	cout << "3. Input a new radius of 3.0 for C1: ";
   	double radiusLength;
	cin >> radiusLength;
	C1.setRadius(radiusLength);
   	cout << "\nHere is C1's new radius:\n" << C1.getRadius() << endl << endl;

	cout << "4. Display the area of C2 by calling fn getArea:\n";
   	cout << "C2's Area: " << C2.getArea() << endl << endl;
   
	cout << "5. Display the diameter of C2 by calling fn getDiameter:\n";
   	cout << "C2's Diameter: " << C2.getDiameter() << endl << endl;
   
	//This was the only non-working part, finally.
	cout << "6. Display the radius and area of C1 by calling fn displayCircle:\n";
   	cout << C1.displayCircle() << endl << endl;
   
	cout << "7. Input a new radius of 10.0 for C1: ";
   	cin >> radiusLength;		
	C1.setRadius(radiusLength);
   	cout << "\nHere is C1's new radius:\n" << C1.getRadius() << endl << endl;

	cout << "8. Display the new circumference of C1 by calling fn getCircumference:\n";
   	cout << "Circumference: " << C1.getCircumference() << endl << endl;

	return 0;
}

To feed it to cout like this

cout << C1.displayCircle() << endl << endl;

you'd need to make an operator<< .

But the way you wrote it,

//CIS 180 Rich Mansfield 0457321 12/7/09
//Lab11.1  -  This program illustrates the use of classes

#include <iostream>
using namespace std;

class Circle
{
private:
   double radius;
   static const double PI;
public:
   Circle();
   Circle(double);

   void   setRadius(double);
   double getRadius() const;
   double getDiameter() const;
   double getCircumference() const;
   double getArea() const;
   void 	 displayCircle() const;
};

const double Circle::PI = 3.1416;

Circle::Circle()//default constructor, inited radius
//Constructors set aside memory but don't name objs
{
   radius = 0.0;
}

Circle::Circle(double r)//arg-passing constructor
//radius inited to r above
{
   radius = r;
}

void Circle::setRadius(double r)
{
   radius = r;
}

double Circle::getRadius() const
{
   return radius;
}

double Circle::getDiameter() const
{
   return 2 * radius;
}

double Circle::getCircumference() const
{
   return 2 * PI * radius;
}

double Circle::getArea() const
{
   return PI * radius * radius;
}

void Circle::displayCircle() const
{
   cout  << "The radius of C1 is "  << radius            << endl
   << ", and its area is " << PI * radius * radius << endl  << endl;
}

int main()
{
   cout << "1. Create a circle object C1 with radius of zero:\n";
   Circle C1;//One req was to have a default constructor set to zero
   cout << "Here is C1's original data:\n";
   cout << "Radius: " << C1.getRadius() << endl << endl;

   cout << "2. Create a circle object C2 with radius of 5.0:\n";
   Circle C2(5.0);
   cout << "Here is C2's original data:\n";
   cout << "Radius: " << C2.getRadius() << endl << endl;

   cout << "3. Input a new radius of 3.0 for C1: ";
   double radiusLength;
   cin >> radiusLength;
   C1.setRadius(radiusLength);
   cout << "\nHere is C1's new radius:\n" << C1.getRadius() << endl << endl;

   cout << "4. Display the area of C2 by calling fn getArea:\n";
   cout << "C2's Area: " << C2.getArea() << endl << endl;

   cout << "5. Display the diameter of C2 by calling fn getDiameter:\n";
   cout << "C2's Diameter: " << C2.getDiameter() << endl << endl;

   //This was the only non-working part, finally.
   cout << "6. Display the radius and area of C1 by calling fn displayCircle:\n";
   C1.displayCircle();

   cout << "7. Input a new radius of 10.0 for C1: ";
   cin >> radiusLength;
   C1.setRadius(radiusLength);
   cout << "\nHere is C1's new radius:\n" << C1.getRadius() << endl << endl;

   cout << "8. Display the new circumference of C1 by calling fn getCircumference:\n";
   cout << "Circumference: " << C1.getCircumference() << endl << endl;

   return 0;
}

... uncomment it back in and call it.

I can't think of a reason why, since the code seems syntactically identical, but 1-5 and 7-8 display fine, and 6 doesn't display at all.

//CIS 180 Rich Mansfield 0457321 12/7/09
//Lab11  -  This program illustrates the use of classes

#include <iostream>
using namespace std;

class Circle
{   
	private:     
		double radius;    
		static const double PI;   
	public:
		Circle();
		Circle(double);

		void   setRadius(double);    
		double getRadius() const;    
		double getDiameter() const;     
		double getCircumference() const;     
		double getArea() const;
                void 	 displayCircle() const;
};    

const double Circle::PI = 3.1416;

Circle::Circle()//default constructor, inited radius
//Constructors set aside memory but don't name objs
{
	radius = 0.0;
}

Circle::Circle(double r)//arg-passing constructor
//radius inited to r above
{
	radius = r;
}

void Circle::setRadius(double r)
{ 
	radius = r; 
}

double Circle::getRadius() const
{ 
	return radius; 
}

double Circle::getDiameter() const
{ 
	return 2 * radius; 
}

double Circle::getCircumference() const
{ 	
	return 2 * PI * radius; 
}

double Circle::getArea() const
{ 	
	return PI * radius * radius; 
}

void Circle::displayCircle() const
{ 	
	cout	<< "The radius of C1 is "	<< radius				<< endl
		<< ", and its area is "	<< PI * radius * radius	<< endl	<< endl; 
}

int main()
{
	cout << "1. Create a circle object C1 with radius of zero:\n";
	Circle C1;
	cout << "Here is C1's original data:\n";
	cout << "Radius: " << C1.getRadius() << endl << endl;

	cout << "2. Create a circle object C2 with radius of 5.0:\n";   
	Circle C2(5.0);
	cout << "Here is C2's original data:\n";
	cout << "Radius: " << C2.getRadius() << endl << endl;   

	cout << "3. Input a new radius of 3.0 for C1: ";
	double radiusLength;
	cin >> radiusLength;
	C1.setRadius(radiusLength);
	cout << "\nHere is C1's new radius: " << C1.getRadius() << endl << endl;

	cout << "4. Display the area of C2 by calling fn getArea:\n";  
	cout << "C2's Area: " << C2.getArea() << endl << endl;

	cout << "5. Display the diameter of C2 by calling fn getDiameter:\n";   
	cout << "C2's Diameter: " << C2.getDiameter() << endl << endl;
	
	cout << "6. Display the radius and area of C1 by calling fn displayCircle:\n";
	C1.displayCircle();

	cout << "7. Input a new radius of 10.0 for C1: ";  
	cin >> radiusLength;		
	C1.setRadius(radiusLength);
	cout << "\nHere is C1's new radius: " << C1.getRadius() << endl << endl;

	cout << "8. Display the new circumference of C1 by calling fn getCircumference:\n";   
	cout << "Circumference: " << C1.getCircumference() << endl << endl;
	return 0;
}

I got output for that line when I ran it:

6. Display the radius and area of C1 by calling fn displayCircle:
The radius of C1 is 3
, and its area is 28.2744

but are you getting any of the parts (like the strings without the values?)

No! Nothing, nada, zip. Here's the output I get:

5. Display the diameter of C2 by calling fn getDiameter:
C2's Diameter: 10

7. Input a new radius of 10.0 for C1: 10

Here is C1's new radius:
10

8. Display the new circumference of C1 by calling fn getCircumference:
Circumference: 62.832

Isn't that weird?

This is one of those things where you'll slap your forehead after.... I can't think of anything other than that you commented it out by mistake (like if you have the ability to comment a block in your IDE). Try putting some other cout statements in and around that area to see if they show.

I think I see what you're saying (though I don't know any other ways to comment out code in the jGrasp IDE except the double slash and the slash star -- star slash). But I tried copying and pasting code snippets 4 and 8 around the code for 6, and what I got was identical to what I had before, nothing between 5 and 7. So the new additional code snippets seem to have disappeared into the same black hole that 6 is in! It's a head-slapper, all right.

cout << "4. Display the area of C2 by calling fn getArea:\n";  
	cout << "C2's Area: " << C2.getArea() << endl << endl;

	cout << "5. Display the diameter of C2 by calling fn getDiameter:\n";   
	cout << "C2's Diameter: " << C2.getDiameter() << endl << endl;
	
	cout << "4. Display the area of C2 by calling fn getArea:\n";  
	cout << "C2's Area: " << C2.getArea() << endl << endl;

	cout << "6. Display the radius and area of C1 by calling fn displayCircle:\n";
	C1.displayCircle();
	
	cout << "8. Display the new circumference of C1 by calling fn getCircumference:\n";   
	cout << "Circumference: " << C1.getCircumference() << endl << endl;
	
	cout << "7. Input a new radius of 10.0 for C1: ";  
	cin >> radiusLength;		
	C1.setRadius(radiusLength);
	cout << "\nHere is C1's new radius: " << C1.getRadius() << endl << endl;

Do you have a debugger on there? (I've used jGrasp for Java in the distant past so I don't remember -- except that it was fussy about filename extensions lol but we knew that) That way you can step through it and try to see where it went. Make sure there's no version problem where it's compiling something from the last time you saved instead of saving what's in the IDE and compiling it.

Good thinking; I re-saved the file under another title and recompiled it, but got the same result. I tried using the debugger, but I'm not sure I really know how to use it, and the results were the same. I'm getting some face time with my prof tomorrow, so I may show it to him; and who knows, it may run better on the school's computers than it does on my Mac. Thanks for sticking with me - if I get satisfaction I'll post it later.

Rich

OK. Good luck with it, wish I could've figured it out.

I finally isolated - to some extent - where the problem is, and it's not the coding. When I start a simple new "Hello, World" program, I get the same old results. The IDE is stuck on that code and won't get off it. I hope I don't have to reformat the disk! I'm going to send a copy of the whole shebang to jGrasp and Apple in the hopes they can tell me how to fix it. Meantime, thanks again for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.