473,543 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
br**@linuxcanad a.com

Jul 23 '05 #1
2 1410
Brad Pepers wrote:
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.


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

Jul 23 '05 #2

"Brad Pepers" <br**@linuxcana da.com> wrote in message
news:eQ4Td.321$ TB.182@edtnps84 ...
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);
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::f ormat(int), as well!
};

Valcon::Valcon( )
{
}

Valcon::~Valcon ()
{
}

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

NumberValcon::N umberValcon()
{
}

NumberValcon:: ~NumberValcon()
{
}

int
NumberValcon::f ormat(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::Mo neyValcon()
{
}

MoneyValcon::~M oneyValcon()
{
}

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

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


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
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6494
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual interest rate I, and annual service charge S. Your algorithm would then compute and print out the total amount of interest earned during the year and...
5
7227
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline...
0
1464
by: samsonfr | last post by:
Hello, I have read KB 814472 and the article "How To: Remove Dependency on _vcclrit.h" (http://msdn2.microsoft.com/en-us/library/ms173267(en-us,vs.80).aspx) which confirmed that VS.NET 2005 don't need the "/noentry + MSVCRT.LIB + __DllMainCRTStartup@12 + __crt_dll_initialize, and __crt_dll_terminate" fix anymore ... I used the article...
3
3078
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { // Write to console
10
4389
by: Brennan Young | last post by:
Hi there, First I'll apologise for my ignorance! I have been attempting to compile some code that I found on the net. AFAICT it's intended to be portable code (it was available as source and as a DOS binary, but mention was made of UNIX in the readme - I am compiling under Mac OS X, fwiw).
26
495
by: jacob navia | last post by:
Summary: I have changed (as proposed by Chuck) the code to use isalpha() instead of (c>='a' && c <= 'z') etc. I agree that EBCDIC exists :-) I eliminated the goto statement, obviously it is better in a tutorial to stick to structured programming whenever possible...
10
1797
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it didn't work; then he installed 2.0 and it still didn't work; so he tried with 2.1 and it didn't work, then 3.0...
176
8194
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the...
2
1369
by: Tony Johansson | last post by:
Hello! I have a very simple example below. The problem is that I get the following compile error Error 1 Cannot implicitly convert type 'System.Reflection.MemberInfo' to 'MemberInfo' F:\C#\ConsoleApplication12\ConsoleApplication12\MemberInfo.cs 27 46 ConsoleApplication12 I can't understand why I get this compile error because according...
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7349
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.