Constructor problem

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

    Constructor problem

    I am in the process of writing a class that will represent metric distances
    by accepting a value (ie, 3) and a unit of measure (ie, m).

    I've written my constructor in the .h file as

    Distance (int, char);

    I've written the constructor in the .cpp implementation file as:

    Distance :: Distance ( int n, char m) : nu(n), me(m) {}

    When I try to enter a value in main using:
    Distance a = Distance (5, m);

    I get an error saying the value "m" is undeclared, yet the 5 is accepted.
    Considering I declared these variables in the initial constructor I can't
    understand why I'm getting this error.

    I'd appreciate it if someone could give me an explanation of what I'm doing
    wrong and how I can correct my mistake.

    Thanks





  • David Harmon

    #2
    Re: Constructor problem

    On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
    wrote,[color=blue]
    >Distance (int, char);[/color]
    [color=blue]
    >Distance a = Distance (5, m);
    >
    >I get an error saying the value "m" is undeclared,[/color]

    It is undeclared. m is a identifier, not a char literal as 'm' would
    be.

    Comment

    • David Harmon

      #3
      Re: Constructor problem

      On Sat, 10 Apr 2004 03:43:59 GMT in comp.lang.c++, "Chiller" <...@...>
      wrote,[color=blue]
      >Distance (int, char);[/color]
      [color=blue]
      >Distance a = Distance (5, m);
      >
      >I get an error saying the value "m" is undeclared,[/color]

      It is undeclared. m is a identifier, not a char literal as 'm' would
      be.

      Comment

      • Thomas Tutone

        #4
        Re: Constructor problem

        "Chiller" <...@...> wrote in message
        news:3d1a35a44a 40e3806950ba940 252df12@news.te ranews.com...
        [color=blue]
        > I am in the process of writing a class that will represent metric[/color]
        distances[color=blue]
        > by accepting a value (ie, 3) and a unit of measure (ie, m).
        >
        > I've written my constructor in the .h file as
        >
        > Distance (int, char);
        >
        > I've written the constructor in the .cpp implementation file as:
        >
        > Distance :: Distance ( int n, char m) : nu(n), me(m) {}
        >
        > When I try to enter a value in main using:
        > Distance a = Distance (5, m);
        >
        > I get an error saying the value "m" is undeclared, yet the 5 is accepted.
        > Considering I declared these variables in the initial constructor I can't
        > understand why I'm getting this error.
        >
        > I'd appreciate it if someone could give me an explanation of what I'm[/color]
        doing[color=blue]
        > wrong and how I can correct my mistake.[/color]

        If you mean the literal character 'm', then you can do:

        Distance a = Distance(5, 'm');

        or, more concisely:

        Distance a(5, 'm');

        If you don't mean the literal character 'm', then you need to be more
        specific about what it is you are trying to do. In particular, provide an
        actual compilable piece of code - what you provided is not compilable as is.

        Best regards,

        Tom


        Comment

        • Thomas Tutone

          #5
          Re: Constructor problem

          "Chiller" <...@...> wrote in message
          news:3d1a35a44a 40e3806950ba940 252df12@news.te ranews.com...
          [color=blue]
          > I am in the process of writing a class that will represent metric[/color]
          distances[color=blue]
          > by accepting a value (ie, 3) and a unit of measure (ie, m).
          >
          > I've written my constructor in the .h file as
          >
          > Distance (int, char);
          >
          > I've written the constructor in the .cpp implementation file as:
          >
          > Distance :: Distance ( int n, char m) : nu(n), me(m) {}
          >
          > When I try to enter a value in main using:
          > Distance a = Distance (5, m);
          >
          > I get an error saying the value "m" is undeclared, yet the 5 is accepted.
          > Considering I declared these variables in the initial constructor I can't
          > understand why I'm getting this error.
          >
          > I'd appreciate it if someone could give me an explanation of what I'm[/color]
          doing[color=blue]
          > wrong and how I can correct my mistake.[/color]

          If you mean the literal character 'm', then you can do:

          Distance a = Distance(5, 'm');

          or, more concisely:

          Distance a(5, 'm');

          If you don't mean the literal character 'm', then you need to be more
          specific about what it is you are trying to do. In particular, provide an
          actual compilable piece of code - what you provided is not compilable as is.

          Best regards,

          Tom


          Comment

          • Chiller

            #6
            Re: Constructor problem

            Is there a way that I can write the constructor so that it will simply
            accept two variables, ie. when I create an object I can do so by simply
            typing

            Distance a = Distance (5, m); or
            Distance a = Distance (5, km); etc

            without having to use ' ' or declaring m, km?

            Thanks


            Comment

            • Chiller

              #7
              Re: Constructor problem

              Is there a way that I can write the constructor so that it will simply
              accept two variables, ie. when I create an object I can do so by simply
              typing

              Distance a = Distance (5, m); or
              Distance a = Distance (5, km); etc

              without having to use ' ' or declaring m, km?

              Thanks


              Comment

              • Mark A. Gibbs

                #8
                Re: Constructor problem

                Chiller wrote:
                [color=blue]
                > Is there a way that I can write the constructor so that it will simply
                > accept two variables, ie. when I create an object I can do so by simply
                > typing
                >
                > Distance a = Distance (5, m); or
                > Distance a = Distance (5, km); etc
                >
                > without having to use ' ' or declaring m, km?[/color]

                yes, but really, i'm still not sure what exactly you're looking for.

                it *sounds* like you want to do something more like this:

                class DistanceUnit
                {
                private:
                DistanceUnit(in t value) :
                v_(value),
                n_(name)
                {}

                DistanceUnit(Di stanceUnit const&);
                void operator=(Dista nceUnit const&);

                int v_;

                friend class Distance;
                };

                class Distance
                {
                public:
                static const DistanceUnit m;
                static const DistanceUnit km;

                Distance(int n, const DistanceUnit& unit) :
                value_(n),
                unit_(unit)
                {}

                private:
                int value_;
                DistanceUnit unit_;
                };

                Distance::Dista nceUnit m(1);
                Distance::Dista nceUnit km(1000);

                to use:

                using Distance::m;
                Distance a(5, m);

                or:

                Distance a(5, Distance::m);

                this is just a *very* rough starting point - i wrote this off the cuff
                without even checking anything (maybe you should put DistanceUnit in
                Distance - that's up to you). unless you're willing to supply more
                information, i can't offer you any more than this. but starting from
                here you can create a system that can handle arbitrary distance units in
                meters, kilometers, millimeters, inches, feet, nautical miles or cubits
                (though you'd have to give up on the ints and go to floats at least),
                can convert automatically between any two of them and (if you're clever
                enough to add a string to DistanceUnit) print out the length in the
                given unit (or any other chosen unit).

                mark

                Comment

                • Mark A. Gibbs

                  #9
                  Re: Constructor problem

                  Chiller wrote:
                  [color=blue]
                  > Is there a way that I can write the constructor so that it will simply
                  > accept two variables, ie. when I create an object I can do so by simply
                  > typing
                  >
                  > Distance a = Distance (5, m); or
                  > Distance a = Distance (5, km); etc
                  >
                  > without having to use ' ' or declaring m, km?[/color]

                  yes, but really, i'm still not sure what exactly you're looking for.

                  it *sounds* like you want to do something more like this:

                  class DistanceUnit
                  {
                  private:
                  DistanceUnit(in t value) :
                  v_(value),
                  n_(name)
                  {}

                  DistanceUnit(Di stanceUnit const&);
                  void operator=(Dista nceUnit const&);

                  int v_;

                  friend class Distance;
                  };

                  class Distance
                  {
                  public:
                  static const DistanceUnit m;
                  static const DistanceUnit km;

                  Distance(int n, const DistanceUnit& unit) :
                  value_(n),
                  unit_(unit)
                  {}

                  private:
                  int value_;
                  DistanceUnit unit_;
                  };

                  Distance::Dista nceUnit m(1);
                  Distance::Dista nceUnit km(1000);

                  to use:

                  using Distance::m;
                  Distance a(5, m);

                  or:

                  Distance a(5, Distance::m);

                  this is just a *very* rough starting point - i wrote this off the cuff
                  without even checking anything (maybe you should put DistanceUnit in
                  Distance - that's up to you). unless you're willing to supply more
                  information, i can't offer you any more than this. but starting from
                  here you can create a system that can handle arbitrary distance units in
                  meters, kilometers, millimeters, inches, feet, nautical miles or cubits
                  (though you'd have to give up on the ints and go to floats at least),
                  can convert automatically between any two of them and (if you're clever
                  enough to add a string to DistanceUnit) print out the length in the
                  given unit (or any other chosen unit).

                  mark

                  Comment

                  • Chiller

                    #10
                    Re: Constructor problem

                    Below I have included the .h file .cpp implementation file. The .cpp file
                    has a TEST_DISTANCE driver that needs to be declared to the preprocessor to
                    allow it to be compiled.

                    I'd greatly appreciate some advice on what I'm doing wrong and how I can
                    correct the problem.

                    Whenever I compile I get the following errors:

                    Distance.cpp(32 ) : error C2556: 'char Distance::measu re(void) const' :
                    overloaded function differs only by return type from 'int
                    Distance::measu re(void) const'

                    Distance.h(25) : see declaration of 'Distance::meas ure'

                    Distance.cpp(32 ) : error C2371: 'Distance::meas ure' : redefinition;
                    different basic types

                    Distance.h(25) : see declaration of 'Distance::meas ure'

                    Distance.cpp(39 ) : error C2264: 'Distance::meas ure' : error in function
                    definition or declaration; function not called



                    ..h file as follows

                    *************** *************** *************** *****

                    #ifndef DISTANCE_H

                    #define DISTANCE_H

                    #include <iostream>

                    using namespace std;

                    class Distance

                    {

                    public :

                    Distance (int, char) ; // constructor - takes int and char values

                    Distance (int) ; // constructor - takes int value

                    Distance (void) ; // default - zero

                    //access member functions

                    int number (void) const;

                    int measure (void) const;


                    private :

                    int nu ; // the value

                    char me ; // the unit of measure (m)

                    } ;

                    // provide an overload of "<<" for easy display

                    ostream& operator<< (ostream&, const Distance&);

                    #endif



                    ..cpp file as follows

                    *************** *************** ***********

                    #include "Distance.h "

                    #include <iostream>

                    #include <string>

                    using namespace std;

                    /*-------------------------------------------------------*\

                    | implementation of member functions |

                    \*-------------------------------------------------------*/

                    // constructor

                    Distance :: Distance (int n, char m) : nu(n), me(m) {}

                    Distance :: Distance (int n) : nu(n) {}

                    Distance :: Distance (void) : nu(0) {}

                    // access functions

                    int Distance :: number (void) const

                    {

                    return nu;

                    }

                    char Distance :: measure (void) const

                    {

                    return me;

                    }

                    // provide an overload of "<<" for easy display

                    ostream& operator<< (ostream& out, const Distance& d)

                    {

                    out << "(" << d.number() << "," << d.measure() << ")" ;

                    return out;

                    }

                    /*-------------------------------------------------------*\

                    | test driver for the Distance class |

                    \*-------------------------------------------------------*/

                    #ifdef TEST_DISTANCE // .... Distance class .... test driver

                    int main (void)

                    {

                    // create test input

                    Distance a = Distance (6);

                    Distance b (4);

                    Distance c (2);

                    Distance d;

                    Distance e (5, 'm');


                    cout << a << endl << b << endl << c << endl << d << endl << e << endl;



                    cin.ignore();

                    return 0; // normal termination

                    }

                    #endif






                    Comment

                    • Chiller

                      #11
                      Re: Constructor problem

                      Below I have included the .h file .cpp implementation file. The .cpp file
                      has a TEST_DISTANCE driver that needs to be declared to the preprocessor to
                      allow it to be compiled.

                      I'd greatly appreciate some advice on what I'm doing wrong and how I can
                      correct the problem.

                      Whenever I compile I get the following errors:

                      Distance.cpp(32 ) : error C2556: 'char Distance::measu re(void) const' :
                      overloaded function differs only by return type from 'int
                      Distance::measu re(void) const'

                      Distance.h(25) : see declaration of 'Distance::meas ure'

                      Distance.cpp(32 ) : error C2371: 'Distance::meas ure' : redefinition;
                      different basic types

                      Distance.h(25) : see declaration of 'Distance::meas ure'

                      Distance.cpp(39 ) : error C2264: 'Distance::meas ure' : error in function
                      definition or declaration; function not called



                      ..h file as follows

                      *************** *************** *************** *****

                      #ifndef DISTANCE_H

                      #define DISTANCE_H

                      #include <iostream>

                      using namespace std;

                      class Distance

                      {

                      public :

                      Distance (int, char) ; // constructor - takes int and char values

                      Distance (int) ; // constructor - takes int value

                      Distance (void) ; // default - zero

                      //access member functions

                      int number (void) const;

                      int measure (void) const;


                      private :

                      int nu ; // the value

                      char me ; // the unit of measure (m)

                      } ;

                      // provide an overload of "<<" for easy display

                      ostream& operator<< (ostream&, const Distance&);

                      #endif



                      ..cpp file as follows

                      *************** *************** ***********

                      #include "Distance.h "

                      #include <iostream>

                      #include <string>

                      using namespace std;

                      /*-------------------------------------------------------*\

                      | implementation of member functions |

                      \*-------------------------------------------------------*/

                      // constructor

                      Distance :: Distance (int n, char m) : nu(n), me(m) {}

                      Distance :: Distance (int n) : nu(n) {}

                      Distance :: Distance (void) : nu(0) {}

                      // access functions

                      int Distance :: number (void) const

                      {

                      return nu;

                      }

                      char Distance :: measure (void) const

                      {

                      return me;

                      }

                      // provide an overload of "<<" for easy display

                      ostream& operator<< (ostream& out, const Distance& d)

                      {

                      out << "(" << d.number() << "," << d.measure() << ")" ;

                      return out;

                      }

                      /*-------------------------------------------------------*\

                      | test driver for the Distance class |

                      \*-------------------------------------------------------*/

                      #ifdef TEST_DISTANCE // .... Distance class .... test driver

                      int main (void)

                      {

                      // create test input

                      Distance a = Distance (6);

                      Distance b (4);

                      Distance c (2);

                      Distance d;

                      Distance e (5, 'm');


                      cout << a << endl << b << endl << c << endl << d << endl << e << endl;



                      cin.ignore();

                      return 0; // normal termination

                      }

                      #endif






                      Comment

                      • John Harrison

                        #12
                        Re: Constructor problem


                        "Chiller" <...@...> wrote in message
                        news:59b0c5bce8 57990952dc322b6 a1458b2@news.te ranews.com...[color=blue]
                        > Is there a way that I can write the constructor so that it will simply
                        > accept two variables, ie. when I create an object I can do so by simply
                        > typing
                        >
                        > Distance a = Distance (5, m); or
                        > Distance a = Distance (5, km); etc
                        >[/color]

                        Its more common to write

                        Distance a(5, m);
                        Distance a(5, km);
                        [color=blue]
                        > without having to use ' ' or declaring m, km?
                        >
                        > Thanks
                        >[/color]

                        Probably what you want is an enum

                        enum
                        {
                        m,
                        km,
                        miles,
                        };

                        etc.

                        Put that at the top of you program and then change char to int. I.e. you
                        constructor becomes

                        Distance (int, int);

                        But I'd seriously consider using longer names than m, km etc. How about

                        enum
                        {
                        units_meters,
                        units_kilometer s,
                        units_miles,
                        };

                        that's how it is normally done.

                        john


                        Comment

                        • John Harrison

                          #13
                          Re: Constructor problem


                          "Chiller" <...@...> wrote in message
                          news:59b0c5bce8 57990952dc322b6 a1458b2@news.te ranews.com...[color=blue]
                          > Is there a way that I can write the constructor so that it will simply
                          > accept two variables, ie. when I create an object I can do so by simply
                          > typing
                          >
                          > Distance a = Distance (5, m); or
                          > Distance a = Distance (5, km); etc
                          >[/color]

                          Its more common to write

                          Distance a(5, m);
                          Distance a(5, km);
                          [color=blue]
                          > without having to use ' ' or declaring m, km?
                          >
                          > Thanks
                          >[/color]

                          Probably what you want is an enum

                          enum
                          {
                          m,
                          km,
                          miles,
                          };

                          etc.

                          Put that at the top of you program and then change char to int. I.e. you
                          constructor becomes

                          Distance (int, int);

                          But I'd seriously consider using longer names than m, km etc. How about

                          enum
                          {
                          units_meters,
                          units_kilometer s,
                          units_miles,
                          };

                          that's how it is normally done.

                          john


                          Comment

                          • John Harrison

                            #14
                            Re: Constructor problem


                            "Chiller" <...@...> wrote in message
                            news:2f1429a771 8931f84a0463892 99dff75@news.te ranews.com...[color=blue]
                            > Below I have included the .h file .cpp implementation file. The .cpp file
                            > has a TEST_DISTANCE driver that needs to be declared to the preprocessor[/color]
                            to[color=blue]
                            > allow it to be compiled.
                            >
                            > I'd greatly appreciate some advice on what I'm doing wrong and how I can
                            > correct the problem.
                            >
                            > Whenever I compile I get the following errors:
                            >
                            > Distance.cpp(32 ) : error C2556: 'char Distance::measu re(void) const' :
                            > overloaded function differs only by return type from 'int
                            > Distance::measu re(void) const'
                            >[/color]

                            [snip]
                            [color=blue]
                            > int measure (void) const;[/color]

                            You declare measure as returning an int here

                            [snip][color=blue]
                            >
                            > char Distance :: measure (void) const[/color]

                            But you define measure and returning a char here. The definition and
                            dclaration must be the same.

                            john


                            Comment

                            • John Harrison

                              #15
                              Re: Constructor problem


                              "Chiller" <...@...> wrote in message
                              news:2f1429a771 8931f84a0463892 99dff75@news.te ranews.com...[color=blue]
                              > Below I have included the .h file .cpp implementation file. The .cpp file
                              > has a TEST_DISTANCE driver that needs to be declared to the preprocessor[/color]
                              to[color=blue]
                              > allow it to be compiled.
                              >
                              > I'd greatly appreciate some advice on what I'm doing wrong and how I can
                              > correct the problem.
                              >
                              > Whenever I compile I get the following errors:
                              >
                              > Distance.cpp(32 ) : error C2556: 'char Distance::measu re(void) const' :
                              > overloaded function differs only by return type from 'int
                              > Distance::measu re(void) const'
                              >[/color]

                              [snip]
                              [color=blue]
                              > int measure (void) const;[/color]

                              You declare measure as returning an int here

                              [snip][color=blue]
                              >
                              > char Distance :: measure (void) const[/color]

                              But you define measure and returning a char here. The definition and
                              dclaration must be the same.

                              john


                              Comment

                              Working...