Help with my code? Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Peterman

    Help with my code? Classes

    I am trying to program a couple classes. One is called PositiveInteger and
    is supposed to just store a positive integer. The second class is called
    Circle, and is meant to describe a circle. Circle is meant to use
    PositiveInteger when describing the circle's radius. Sorry if haven't
    described it properly.

    Could someone take a look at my code and tell me where I am going wrong, and
    why it keeps giving me this error

    error C2533: 'Circle::Circle ' : constructors not allowed a return type

    Thanks.
    I'll post the 4 code files as replies to this message.


  • J Peterman

    #2
    Re: Help with my code? Classes

    // file: PositiveInteger .h

    #ifndef POSITIVEINTEGER _H
    #define POSITIVEINTEGER _H

    //-------------------------------------------------------------------

    #include <iostream.h>

    //-------------------------------------------------------------------

    class PositiveInteger
    {
    public:
    /* constructors */
    PositiveInteger (); // default
    PositiveInteger (const PositiveInteger &posInt); // copy
    ~PositiveIntege r() {}; // destructor
    void Clear(); // clear value to 0

    /* set method */
    bool SetValue(int);

    /* get method */
    int GetValue() const {return m_value;}

    /* overload operators */
    PositiveInteger &operator = (const PositiveInteger &posInt);
    bool operator < (const PositiveInteger &posInt);
    bool operator > (const PositiveInteger &posInt);
    bool operator == (const PositiveInteger &posInt);

    /* input/output methods */
    friend ostream& operator << (ostream &ostr, const PositiveInteger &posInt);
    friend istream& operator >> (istream &istr, PositiveInteger &posInt);

    private:
    int m_value;
    };

    #endif;


    Comment

    • J Peterman

      #3
      Re: Help with my code? Classes

      // file: PositiveInteger .cpp

      #include "PositiveIntege r.h"

      //-------------------------------------------------------------------

      PositiveInteger ::PositiveInteg er()
      {
      Clear();
      }

      //-------------------------------------------------------------------

      PositiveInteger ::PositiveInteg er(const PositiveInteger &posInt)
      {
      SetValue(posInt .m_value);
      }

      //-------------------------------------------------------------------
      /*
      PositiveInteger ::~PositiveInte ger()
      {
      // do nothing
      }
      */
      //-------------------------------------------------------------------

      void PositiveInteger ::Clear()
      {
      SetValue(0);
      }

      //-------------------------------------------------------------------

      bool PositiveInteger ::SetValue(int value)
      {
      if (value < 0)
      {
      m_value = 0; // if negative, set to 0
      return false;
      }
      else
      {
      m_value = value;
      return true;
      }
      }

      //-------------------------------------------------------------------

      PositiveInteger &PositiveIntege r::operator = (const PositiveInteger &posInt)
      {
      if (this != &posInt)
      SetValue(posInt .m_value);

      return *this;
      }

      //-------------------------------------------------------------------

      bool PositiveInteger ::operator < (const PositiveInteger &posInt)
      {
      if (m_value < posInt.m_value)
      return true;
      else
      return false;
      }

      //-------------------------------------------------------------------

      bool PositiveInteger ::operator > (const PositiveInteger &posInt)
      {
      if (m_value > posInt.m_value)
      return true;
      else
      return false;
      }

      //-------------------------------------------------------------------

      bool PositiveInteger ::operator == (const PositiveInteger &posInt)
      {
      if (m_value == posInt.m_value)
      return true;
      else
      return false;
      }

      //-------------------------------------------------------------------

      ostream& operator << (ostream &ostr, const PositiveInteger &posInt)
      {
      ostr << "Value: " << posInt.m_value;
      return ostr;
      }

      //-------------------------------------------------------------------

      istream& operator << (istream &istr, PositiveInteger &posInt)
      {
      int value;

      do
      {
      cout << "Enter a positive integer: ";
      istr >> value;
      }while(!posInt. SetValue(value) );
      return istr;

      }

      //-------------------------------------------------------------------


      Comment

      • J Peterman

        #4
        Re: Help with my code? Classes

        // file: Circle.h

        #ifndef CIRCLE_H
        #define CIRCLE_H

        //-------------------------------------------------------------------

        #include <iostream.h>
        #include "PositiveIntege r.h"

        //-------------------------------------------------------------------

        class Circle
        {
        public:
        /* constructors */
        Circle(); // default
        Circle(int radius, int x, int y);
        Circle(const Circle &circle); //copy
        ~Circle() {}; // destructor
        void Clear(); // clear circle to 0.....not sure how it works?

        /* set methods */
        bool SetRadius(int radius);
        bool SetX(int);
        bool SetY(int);

        /* get methods */
        int GetRadius() {return m_radius.GetVal ue();}
        int GetX() const {return m_x;}
        int GetY() const {return m_y;}
        double GetPerimeter();
        double GetArea();//what to do for these?

        /* input/output methods */
        //friend ostream& operator << (ostream &ostr, const Circle &circle);
        //friend istream& operator >> (istream &istr, Circle &circle);

        private:
        PositiveInteger m_radius;
        int m_x;
        int m_y;
        }

        #endif


        Comment

        • J Peterman

          #5
          Re: Help with my code? Classes

          // file: Circle.cpp

          #include "Circle.h"

          //-------------------------------------------------------------------

          Circle::Circle( )
          {
          Clear();
          }

          //-------------------------------------------------------------------

          Circle::Circle( int radius, int x, int y)
          {
          SetRadius(radiu s);
          SetX(x);
          SetY(y);
          }

          //-------------------------------------------------------------------

          Circle::Circle( const Circle &circle)
          {
          SetRadius(circl e.m_radius.GetV alue());
          SetX(circle.m_x );
          SetY(circle.m_y );
          }

          //-------------------------------------------------------------------

          void Circle::Clear()
          {
          m_radius.SetVal ue(0);
          SetRadius(0);
          SetX(0);
          SetY(0);
          }

          //-------------------------------------------------------------------

          bool Circle::SetRadi us(int radius)
          {
          if (m_radius.SetVa lue(radius))
          {
          m_radius.SetVal ue(radius);
          return true;
          }
          else
          return false;
          }

          //-------------------------------------------------------------------

          bool Circle::SetX(in t x)
          {
          m_x = x;
          return true;
          }

          //-------------------------------------------------------------------

          bool Circle::SetY(in t y)
          {
          m_y = y;
          return true;
          }

          //-------------------------------------------------------------------



          Comment

          • Karl Heinz Buchegger

            #6
            Re: Help with my code? Classes



            J Peterman wrote:[color=blue]
            >
            > I am trying to program a couple classes. One is called PositiveInteger and
            > is supposed to just store a positive integer. The second class is called
            > Circle, and is meant to describe a circle. Circle is meant to use
            > PositiveInteger when describing the circle's radius. Sorry if haven't
            > described it properly.
            >
            > Could someone take a look at my code and tell me where I am going wrong, and
            > why it keeps giving me this error
            >
            > error C2533: 'Circle::Circle ' : constructors not allowed a return type
            >[/color]

            It would help if you indicate *which* file gave that error.

            But loook in Circle.h
            You lost the ';' to end the class declaration

            class Circle
            {
            ....
            } ;

            ^
            |
            missing

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Sam Holden

              #7
              Re: Help with my code? Classes

              On Wed, 17 Sep 2003 16:39:45 +0800, J Peterman <mrpeterman@hot mail.com> wrote:
              [snip][color=blue]
              > class Circle
              > {[/color]
              [snip public section][color=blue]
              >
              > private:
              > PositiveInteger m_radius;
              > int m_x;
              > int m_y;
              > }[/color]
              ^
              A character is missing here. The PositiveInteger .h has that character,
              so it shouldn't be hard to work out :)

              --
              Sam Holden

              Comment

              • J Peterman

                #8
                Re: Help with my code? Classes

                Thanks a lot!
                I knew it would be something stupid like that. I just couldn't see it...

                "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                news:3F682361.D 3D2152C@gascad. at...[color=blue]
                >
                >
                > J Peterman wrote:[color=green]
                > >
                > > I am trying to program a couple classes. One is called PositiveInteger[/color][/color]
                and[color=blue][color=green]
                > > is supposed to just store a positive integer. The second class is called
                > > Circle, and is meant to describe a circle. Circle is meant to use
                > > PositiveInteger when describing the circle's radius. Sorry if haven't
                > > described it properly.
                > >
                > > Could someone take a look at my code and tell me where I am going wrong,[/color][/color]
                and[color=blue][color=green]
                > > why it keeps giving me this error
                > >
                > > error C2533: 'Circle::Circle ' : constructors not allowed a return type
                > >[/color]
                >
                > It would help if you indicate *which* file gave that error.
                >
                > But loook in Circle.h
                > You lost the ';' to end the class declaration
                >
                > class Circle
                > {
                > ....
                > } ;
                >
                > ^
                > |
                > missing
                >
                > --
                > Karl Heinz Buchegger
                > kbuchegg@gascad .at[/color]


                Comment

                • Patrick Kowalzick

                  #9
                  Re: Help with my code? Classes

                  Hi all,

                  wasn't the destructor implemented inline inside the class?

                  Should produce a compile error, or not?

                  Reagrds,
                  Patrick
                  [color=blue]
                  > //-------------------------------------------------------------------
                  > /*
                  > PositiveInteger ::~PositiveInte ger()
                  > {
                  > // do nothing
                  > }
                  > */
                  > //-------------------------------------------------------------------[/color]


                  Comment

                  • J Peterman

                    #10
                    Re: Help with my code? Classes

                    Sorry, I am not exactly sure what I am doing...still new to this. If I'm not
                    mistaken, the destructor is commented cos I wasn't sure if I needed it or
                    not..

                    "Patrick Kowalzick" <Patrick.Kowalz ick@cern.ch> wrote in message
                    news:bk9cfl$jnb $1@sunnews.cern .ch...[color=blue]
                    > Hi all,
                    >
                    > wasn't the destructor implemented inline inside the class?
                    >
                    > Should produce a compile error, or not?
                    >
                    > Reagrds,
                    > Patrick
                    >[color=green]
                    > > //-------------------------------------------------------------------
                    > > /*
                    > > PositiveInteger ::~PositiveInte ger()
                    > > {
                    > > // do nothing
                    > > }
                    > > */
                    > > //-------------------------------------------------------------------[/color]
                    >
                    >[/color]


                    Comment

                    • jeffc

                      #11
                      Re: Help with my code? Classes


                      "J Peterman" <mrpeterman@hot mail.com> wrote in message
                      news:bk9bnt$k0a $1@nnrp.waia.as n.au...[color=blue]
                      > Thanks a lot!
                      > I knew it would be something stupid like that. I just couldn't see it...[/color]

                      That is a tough error to find as the compiler message will always be
                      misleading to you. But, after some experience, you'll be able to spot this
                      error even based on funny error messages.


                      Comment

                      • Mike Wahler

                        #12
                        Re: Help with my code? Classes


                        jeffc <nobody@nowhere .com> wrote in message
                        news:3f686ee9_4 @news1.prserv.n et...[color=blue]
                        >
                        > "J Peterman" <mrpeterman@hot mail.com> wrote in message
                        > news:bk9bnt$k0a $1@nnrp.waia.as n.au...[color=green]
                        > > Thanks a lot!
                        > > I knew it would be something stupid like that. I just couldn't see it...[/color]
                        >
                        > That is a tough error to find as the compiler message will always be
                        > misleading to you. But, after some experience, you'll be able to spot[/color]
                        this[color=blue]
                        > error even based on funny error messages.[/color]

                        Even better, develop coding habits that prevent this from
                        happening at all. E.g. when you define a class, write the
                        entire 'skeleton' first, e.g.

                        class MyClass
                        {
                        };

                        Then go back and fill it in.

                        Much easier to visually verify that all the syntax is
                        correct than when your class gets full of declarations, etc.

                        -Mike



                        Comment

                        • J Peterman

                          #13
                          Re: Help with my code? Classes

                          [color=blue]
                          > Even better, develop coding habits that prevent this from
                          > happening at all. E.g. when you define a class, write the
                          > entire 'skeleton' first, e.g.
                          >
                          > class MyClass
                          > {
                          > };
                          >
                          > Then go back and fill it in.
                          >
                          > Much easier to visually verify that all the syntax is
                          > correct than when your class gets full of declarations, etc.
                          >
                          > -Mike[/color]

                          Yeah, I started doing that! Thanks!


                          Comment

                          • Patrick Kowalzick

                            #14
                            Re: Help with my code? Classes

                            > > > //-------------------------------------------------------------------[color=blue][color=green][color=darkred]
                            > > > /*
                            > > > PositiveInteger ::~PositiveInte ger()
                            > > > {
                            > > > // do nothing
                            > > > }
                            > > > */
                            > > > //-------------------------------------------------------------------[/color][/color][/color]

                            oh, sorry, overseen the comment. I miss syntax-highligthing for my
                            newsreader ;-).

                            Patrick


                            Comment

                            • Kevin Goodsell

                              #15
                              Re: Help with my code? Classes

                              J Peterman wrote:
                              [color=blue][color=green]
                              >>Even better, develop coding habits that prevent this from
                              >>happening at all. E.g. when you define a class, write the
                              >>entire 'skeleton' first, e.g.
                              >>
                              >>class MyClass
                              >>{
                              >>};
                              >>
                              >>Then go back and fill it in.
                              >>
                              >>Much easier to visually verify that all the syntax is
                              >>correct than when your class gets full of declarations, etc.
                              >>
                              >>-Mike[/color]
                              >
                              >
                              > Yeah, I started doing that! Thanks!
                              >
                              >[/color]

                              Also, always look *very closely* at the 2 or 3 lines ABOVE the line an
                              error is reported on. Sometimes those lines are not in the same file,
                              though.

                              -Kevin
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              Working...