• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Real C++ compile problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Real C++ compile problems


  • Subject: Re: Real C++ compile problems
  • From: robus <email@hidden>
  • Date: Sun, 27 Jun 2004 09:02:02 +0200

(1) General:

For the .h files you should always write

    #ifndef THEFILE_H
    #define THEFILE_H
    ....
    #endif

THEFILE_H stands for CIRCLE_H, SQUARE_H, etc.
This takes care that the files are not included twice and therefore the
classes are not declared twice.
------------------------------------------------------------
(2) circle.h:

Tip: No need to define pi yourself. Just use

    double pi = acos(0.0) * 2;

and pi is calculated correctly and gets as many places as a double can
hold.

I wouldn't use 0.0 for the default value in the constructor of the
circle class. A circle with the radius 0 is not a circle - it's just a
point. I would take 1.0 - simply one unit in the measure system - but
this does not really matter in the code here.
------------------------------------------------------------
(3) square.h

Scratch the line #include "rectangle.h" - it's not needed (it's the
same code as in square.h anyways !?).
In the private section of the class you write

    rectangle sq;

You take rectangle as a type, but you have not defined this type. Since
the class is named "square", let's define a square; this is done by
defining just one side. So you could write

    double side;

Additionally, you should modify the methods. All of them, except
square::diagonal(), are incorrect, or else I maybe just don't see what
you are trying to do. Have a look at my code how I have changed them
according to my understanding.
------------------------------------------------------------
(4) rectangle.h

This code can be omitted entirely. As far as I can see it is the same
code as in square.h, especially, there is the same class name - and
this leads to a name conflct !!

//==========//
// circle.h //
//==========//

#ifndef CIRCLE_H
#define CIRCLE_H

#include <cmath>  // for acos()

const double pi = acos(0.0) * 2; // define pi

class circle
{
   public:
     circle( double r = 1.0 ) : radius(r)
     {}

     double getRadius() const
     { return radius; }

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

     double area() const
     { return pi * radius * radius; }

     double circumference() const
     { return 2.0 * pi * radius; }

   private:
     double radius;
};

#endif

//==========//
// square.h //
//==========//

#ifndef SQUARE_H
#define SQUARE_H

#include <cmath>  // for sqrt()

class square
{
   public:
     square( double len );

     double getSide() const;
     void setSide( double len );

     double perimeter() const;
     double area() const;
     double diagonal() const;
   private:
     double side;
};

square::square( double len = 1.0 ) : side(len)
{}

double square::getSide() const
{ return side; }

void square::setSide( double len )
{ side = len; }

double square::area() const
{ return side * side; }

double square::diagonal() const
{ return sqrt(2.0) * getSide(); }

double square::perimeter() const
{ return 4 * side; }

#endif


//==========//
// main.cpp //
//==========//

#include <iostream>
//#include <cmath>    // can be omitted - not needed for the main()
function
#include "circle.h"
#include "square.h"
using namespace std;

int main ()
{
   square innerSq( 4 );
   circle circ( innerSq.diagonal() / 2 );
   square outerSq( circ.getRadius() * 2 );

   cout << "The area of the crossline section is: "
        << circ.area() - innerSq.area() << endl;
   cout << "The perimeter of the shaded region is: "
        << outerSq.perimeter() << endl;

   return 0;
}


> Thank you all for your initial help with Xcode and seeing the flaws in
> my code. Sometimes when you write it yourself your blind to your own
> mistakes.
>
> But THIS time Im having a confusing error. My program compiles and
> runs fine in unix, whereas in Xcode my errors are:
>
> square.h:32: error: call of overloaded `sqrt(int)' is ambiguous
> math.h:302: error: candidates are: double sqrt(double)
> cmath:461: error:                 float std::sqrt(float)
> cmath:465: error:                 long double std::sqrt(long double)
>
> It looks like its having trouble with my sqrt calls. I dont know why
> its saying the call is overloaded. Also I didnt write the math.h or
> cmath.h programs, so I hope theres no errors there.
>
>
> Im also wondering if Im starting up Xcode correctly for C++ programs.
> When I do File>New Project, I end up selecting "Tools>C++ Tool". Im
> not sure if this is the best way to start. And ALSO *in Xcode* when Im
> writing a linked class,  I choose File>New File then select
> "Carbon>C++ Class".  I hope this is correct. I put all my code into
> the additional *.h files that are created in the class, but Im not
> sure what to do about the *.m files which seem to be linked to the *.h
> files anyway. So I leave them blank.
>
>
> Im including my code for my main program called 2_21.c and all the
> classes that go with it (square.h, rectangle.h, and circle.h)
> -----
> //2_21.c
>
> #include <iostream>
> #include "circle.h"
> #include "square.h"
> #include<cmath>
>
> using namespace std;
>
> int main ()
> {
>         square innerSq(4);
>         circle circ(innerSq.diagonal()/2);
>         square outerSq(circ.getRadius()*2);
>         cout<<"The area of the crossline section is:
> "<<circ.area()-innerSq.area()<<endl;
>         cout<<"The perimeter of the shaded region is:
> "<<outerSq.perimeter()<<endl;
>
>         return 0;
>
> }
> ---------------------------------------
> //square.h
> #include"rectangle.h"
> #include<cmath>
>
> class square
>  {
> public:
> 	square(double len);
>
> 	double getSide() const;
> 	void setSide(double len);
>
> 	double perimeter() const;
> 	double area() const;
> 	double diagonal() const;
> private:
> 	rectangle sq;
> };
>
> square::square(double len):sq(len, len)
> {}
>
> double square::getSide() const
> {return sq.getLength();}
>
> void square::setSide(double len)
> {sq.setSides(len,len);}
>
> double square::area() const
> {return sq.area();}
>
> double square::diagonal() const
> {return sqrt(2)*getSide();}
>
> double square::perimeter() const
> {return sq.perimeter();}
>
> --------------------------
> //rectangle.h
> #include"rectangle.h"
> #include<cmath>
>
> class square
>  {
> public:
> 	square(double len);
>
> 	double getSide() const;
> 	void setSide(double len);
>
> 	double perimeter() const;
> 	double area() const;
> 	double diagonal() const;
> private:
> 	rectangle sq;
> };
>
> square::square(double len):sq(len, len)
> {}
>
> double square::getSide() const
> {return sq.getLength();}
>
> void square::setSide(double len)
> {sq.setSides(len,len);}
>
> double square::area() const
> {return sq.area();}
>
> double square::diagonal() const
> {return sqrt(2)*getSide();}
>
> double square::perimeter() const
> {return sq.perimeter();}
>
> -------------------
> //circle.h
>
> const double pi = 3.141592653589793;
>
> class circle
> {
> public:
>  circle(double r = 0.0): radius(r)
>  {}
>
>  double getRadius() const
>  {return radius;}
>
>  void setRadius(double r)
>  {radius = r;}
>
>  double area() const
>  {return pi*radius*radius;}
>
>  double circumference() const
>  {return 2.0*pi*radius;}
>
> private:
>   double radius;
> };
>
> ------------
>
>
>
> THANKS SO MUCH!!!
> oliver
> The most personalized portal on the Web!
> _______________________________________________
> xcode-users mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/xcode-users
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


References: 
 >Real C++ compile problems (From: "email@hidden" <email@hidden>)

  • Prev by Date: Re: Real C++ compile problems
  • Next by Date: Displaying contents of NSArray and family in debugger
  • Previous by thread: Real C++ compile problems
  • Next by thread: Re: Real C++ compile problems
  • Index(es):
    • Date
    • Thread