why does this not compile ?

W

wim delvaux

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 ?
 
A

Artie Gold

wim said:
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
 
J

Jeff Schwab

wim said:
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 );
}
 
E

E. Robert Tisdale

Artie said:
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
 
A

Artie Gold

wim said:
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
 

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

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top