473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

why does this not compile ?

class A {

public :

virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};

class B : public A {

public :

virtual void a( int x ) { printf( "a with\n" ); }

};

int main( void ) {

B SomeB;

SomeB.a();
SomeB.a( 1 );
}

The error I get with gcc 3.4 is

t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)

What is going on ? Why can't I define two 'a' with different arguments ?
Jul 23 '05 #1
4 1344
wim delvaux wrote:
class A {

public :

virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};

class B : public A {

public :

virtual void a( int x ) { printf( "a with\n" ); }

};

int main( void ) {

B SomeB;

SomeB.a();
SomeB.a( 1 );
}

The error I get with gcc 3.4 is

t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)

What is going on ? Why can't I define two 'a' with different arguments ?


The overloading is fine. The problem you're seeing is due to the fact
that C++ only does dynamic dispatch on a pointer to object or a
reference to object, not on an object itself.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
wim delvaux wrote:
class A {

public :

virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};

class B : public A {

public :

virtual void a( int x ) { printf( "a with\n" ); }

};

int main( void ) {

B SomeB;

SomeB.a();
SomeB.a( 1 );
}

The error I get with gcc 3.4 is

t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)

What is going on ? Why can't I define two 'a' with different arguments ?


Once you've overloaded one version of "a", it masks all other versions
inherited from base classes, even if they have different signatures.
This can be a life-saver, since if you're overloading one version of a
method, there's a good chance you mean to overload others. You can tell
the compiler you really do want to use the base class definition for one
version, but the overloaded definition for another, with a "using
declaration", e.g. "using A::a;".
#include <cstdio>

using std::printf;

class A {
public :
virtual void a( int x ) = 0;

virtual void a( void ) {
printf( "a without\n" );
}
};

class B : public A {
public :
using A::a; // A "using declaration."

virtual void a( int x ) { printf( "a with\n" ); }
};

int main( void ) {

B SomeB;

SomeB.a();
SomeB.a( 1 );
}
Jul 23 '05 #3
wim delvaux wrote:
class A {

public :

virtual void a( int x ) = 0;
virtual void a( void ) {
printf( "a without\n" );
}
};

class B : public A {

public :

virtual void a( int x ) { printf( "a with\n" ); }

};

int main( void ) {

B SomeB;

SomeB.a();
SomeB.a( 1 );
}

The error I get with gcc 3.4 is

t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)

What is going on ? Why can't I define two 'a' with different arguments ?


Please pardon my cerebral flatulence elsethread.
<slinks away slowly>

--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #4
Artie Gold wrote:
wim delvaux wrote:
class A {
public :
virtual void a(int x) = 0;
virtual void a(void) {
printf("a without\n");
}
};

class B: public A {
public :
virtual void a(int x) {
printf( "a with\n" );
}
};

int main(void) {
B SomeB;

SomeB.a();
SomeB.a(1);
}

The error I get with gcc 3.4 is

t.cpp: In function `int main()':
t.cpp:25: error: no matching function for call to `B::a()'
t.cpp:17: error: candidates are: virtual void B::a(int)

What is going on ? Why can't I define two 'a' with different arguments ?
The overloading is fine. The problem you're seeing is due to the fact
that C++ only does dynamic dispatch on a pointer to object or a
reference to object, not on an object itself.


Then why does this work?
cat t.cpp #include <iostream>

class A {
public:
virtual void a(int x) = 0;
void z(void) {
std::cout << "a without" << std::endl;
}
};

class B: public A {
public:
virtual void a(int x) {
std::cout << "a with" << std::endl;
}
};

int main(int argc, char* argv[]) {
B someB;

someB.z();
someB.a(1);
return 0;
}
g++ -Wall -ansi -pedantic -o t t.cpp
./t

a without
a with
Jul 23 '05 #5

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

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
5
by: Jeff | last post by:
hi asp.net 2.0 I get this compile error: 'Image' does not contain a definition for 'ImageUrl' Image image = (Image)e.Item.FindControl("img"); image.ImageUrl = "~/image.png";
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.