By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

why does this not compile ?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.