Simple code compile problem?

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

    Simple code compile problem?

    For the life of me I can't see why this code won't compile even though its
    very simple. Any hints? I get errors in NumberValcon::f ormat(int) when it
    tries to call format() but why doesn't it use the format() in the parent
    Valcon class? Also there is an error in the main() code when trying to use
    MoneyValcon::fo rmat(int) but why doesn't main use the one from NumberValcon
    which is the parent of MoneyValcon? This is on all SuSE 9.2 using g++
    version 3.3.4.

    Here is the code:

    class Valcon {
    public:
    Valcon();
    virtual ~Valcon();

    virtual int format()=0;
    };

    class NumberValcon: public Valcon {
    public:
    NumberValcon();
    ~NumberValcon() ;

    int format(int foo);

    protected:
    int _foo;
    };

    class MoneyValcon: public NumberValcon {
    public:
    MoneyValcon();
    ~MoneyValcon();

    int format();
    };

    Valcon::Valcon( )
    {
    }

    Valcon::~Valcon ()
    {
    }

    int
    Valcon::format( )
    {
    return 0;
    }

    NumberValcon::N umberValcon()
    {
    }

    NumberValcon:: ~NumberValcon()
    {
    }

    int
    NumberValcon::f ormat(int foo)
    {
    _foo = foo;
    return format();
    }

    MoneyValcon::Mo neyValcon()
    {
    }

    MoneyValcon::~M oneyValcon()
    {
    }

    int
    MoneyValcon::fo rmat()
    {
    return 5;
    }

    int main()
    {
    MoneyValcon foo;
    foo.format(10);
    return 0;
    }

    The compile errors are:

    bpepers@deth:~> g++ -c -Wall foo1.cpp
    foo1.cpp: In member function `int NumberValcon::f ormat(int)':
    foo1.cpp:54: error: no matching function for call to
    `NumberValcon:: format()'
    foo1.cpp:52: error: candidates are: int NumberValcon::f ormat(int)
    foo1.cpp: In function `int main()':
    foo1.cpp:74: error: no matching function for call to
    `MoneyValcon::f ormat(int)'
    foo1.cpp:67: error: candidates are: virtual int MoneyValcon::fo rmat()
    distcc[25591] ERROR: compile foo1.cpp on localhost failed

    --
    Brad Pepers
    brad@linuxcanad a.com

  • Rolf Magnus

    #2
    Re: Simple code compile problem?

    Brad Pepers wrote:
    [color=blue]
    > For the life of me I can't see why this code won't compile even though its
    > very simple. Any hints? I get errors in NumberValcon::f ormat(int) when
    > it tries to call format() but why doesn't it use the format() in the
    > parent Valcon class? Also there is an error in the main() code when
    > trying to use MoneyValcon::fo rmat(int) but why doesn't main use the one
    > from NumberValcon which is the parent of MoneyValcon? This is on all SuSE
    > 9.2 using g++ version 3.3.4.[/color]

    A function in a derived class hides all the functions of the base class that
    have the same name.

    Comment

    • Howard

      #3
      Re: Simple code compile problem?


      "Brad Pepers" <brad@linuxcana da.com> wrote in message
      news:eQ4Td.321$ TB.182@edtnps84 ...[color=blue]
      > For the life of me I can't see why this code won't compile even though its
      > very simple. Any hints? I get errors in NumberValcon::f ormat(int) when
      > it
      > tries to call format() but why doesn't it use the format() in the parent
      > Valcon class? Also there is an error in the main() code when trying to
      > use
      > MoneyValcon::fo rmat(int) but why doesn't main use the one from
      > NumberValcon
      > which is the parent of MoneyValcon? This is on all SuSE 9.2 using g++
      > version 3.3.4.
      >
      > Here is the code:
      >
      > class Valcon {
      > public:
      > Valcon();
      > virtual ~Valcon();
      >
      > virtual int format()=0;
      > };
      >
      > class NumberValcon: public Valcon {
      > public:
      > NumberValcon();
      > ~NumberValcon() ;
      >
      > int format(int foo);[/color]

      This does not override Valcon::format( ), because it has different
      parameters. Instead, it *hides* Valcon::format( ).
      [color=blue]
      >
      > protected:
      > int _foo;
      > };
      >
      > class MoneyValcon: public NumberValcon {
      > public:
      > MoneyValcon();
      > ~MoneyValcon();
      >
      > int format();[/color]

      Now you're hiding NumberValcon::f ormat(int), as well!
      [color=blue]
      > };
      >
      > Valcon::Valcon( )
      > {
      > }
      >
      > Valcon::~Valcon ()
      > {
      > }
      >
      > int
      > Valcon::format( )
      > {
      > return 0;
      > }
      >
      > NumberValcon::N umberValcon()
      > {
      > }
      >
      > NumberValcon:: ~NumberValcon()
      > {
      > }
      >
      > int
      > NumberValcon::f ormat(int foo)
      > {
      > _foo = foo;
      > return format();[/color]

      If you *need* to do it this way, then you need to specify the class that
      this function is from. But even if you add the Valcon:: specifier, it won't
      work, because you have made Valcon::format( ) a pure virtual function (with
      the "=0" following the declaration).


      [color=blue]
      > }
      >
      > MoneyValcon::Mo neyValcon()
      > {
      > }
      >
      > MoneyValcon::~M oneyValcon()
      > {
      > }
      >
      > int
      > MoneyValcon::fo rmat()
      > {
      > return 5;
      > }
      >
      > int main()
      > {
      > MoneyValcon foo;
      > foo.format(10);[/color]

      MoneyValcon::fo rmat() has no parameters, so you get an error. Your format
      function has to have the same parameter list if you want to use the classes
      polymorphically .

      -Howard


      Comment

      Working...