Simple code compile problem?

B

Brad Pepers

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::format(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::format(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::NumberValcon()
{
}

NumberValcon:: ~NumberValcon()
{
}

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

MoneyValcon::MoneyValcon()
{
}

MoneyValcon::~MoneyValcon()
{
}

int
MoneyValcon::format()
{
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::format(int)':
foo1.cpp:54: error: no matching function for call to
`NumberValcon::format()'
foo1.cpp:52: error: candidates are: int NumberValcon::format(int)
foo1.cpp: In function `int main()':
foo1.cpp:74: error: no matching function for call to
`MoneyValcon::format(int)'
foo1.cpp:67: error: candidates are: virtual int MoneyValcon::format()
distcc[25591] ERROR: compile foo1.cpp on localhost failed
 
R

Rolf Magnus

Brad said:
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::format(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::format(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.

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

Howard

Brad Pepers said:
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::format(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::format(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);

This does not override Valcon::format(), because it has different
parameters. Instead, it *hides* Valcon::format().
protected:
int _foo;
};

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

int format();

Now you're hiding NumberValcon::format(int), as well!
};

Valcon::Valcon()
{
}

Valcon::~Valcon()
{
}

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

NumberValcon::NumberValcon()
{
}

NumberValcon:: ~NumberValcon()
{
}

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

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).


}

MoneyValcon::MoneyValcon()
{
}

MoneyValcon::~MoneyValcon()
{
}

int
MoneyValcon::format()
{
return 5;
}

int main()
{
MoneyValcon foo;
foo.format(10);

MoneyValcon::format() 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top