Is this a bug in g++ or MSDEV?

J

jesse

#include <string>
#include <iostream>

template <typename T>
class Base
{
public:
Base(){
};
~Base(){
};

void talk(){
std::cout << " this is base talk" << std::endl;
};

void talk(bool flag){
std::cout << " this is base talk with flag" << std::endl;
};
void sing(){
std::cout << " this is base sing" << std::endl;
};

};
template <typename T>
class Derived:public Base<T>
{
public:
Derived():Base<T>()
{

};
~Derived()
{
};
};
template <>
class Derived<int>:public Base<int>
{
public:
Derived():Base<int>()
{
talk(); // <--------------- Location A;
talk(false); // <--------------- Location B;
sing(); // <--------------- Location C;
};
~Derived()
{
};
void talk(){
std::cout << " this is derived talk" << std::endl;
};
};


int main(void)
{
Derived<int> m;
m.talk();
return 0;
}

when i compile it with g++, i get the following error:

/*
* mytest.cpp: In constructor `Derived<int>::Derived()':
* mytest.cpp:43: no matching function for call to
`Derived<int>::talk(bool)'
* mytest.cpp:49: candidates are: void Derived<int>::talk()
*/

I just wondering why in location C, the method sing() can be called
while talk(false) can not be called since both are in the base class.

when i compile it with MS VStudio. i get similar error:

Compiling...
mytest.cpp
D:\code\c++\TestGcc\mytest.cpp(43) : error C2660: 'talk' : function does
not take 1 parameters
Error executing cl.exe.

TestGcc.exe - 1 error(s), 0 warning(s)


is this a bug of implementation in compiler?
 
L

lilburne

jesse said:
Compiling...
mytest.cpp
D:\code\c++\TestGcc\mytest.cpp(43) : error C2660: 'talk' : function does
not take 1 parameters
Error executing cl.exe.

TestGcc.exe - 1 error(s), 0 warning(s)


is this a bug of implementation in compiler?

Neither your definition of talk() in Derived has hidden the
definitions in Base.
 
J

jesse

I don't think so, i only overwrite the : void talk();
however, i didn't overwrite: void talk(bool flag)
their signatures are different.
 
A

Andrey Tarasevich

jesse said:
I don't think so, i only overwrite the : void talk();
however, i didn't overwrite: void talk(bool flag)
their signatures are different.

The signatures make no difference. Once you declare a function called
'talk' in the derived class, it hides all 'talk' functions from the base
class regardless of their signatures. You can "unhide" them with a
using-declaration.
 
J

jesse

Why Compiler design this this way? i don't see any pitfall or particular
reason?

at least, in java, it seems no hiding existing.

BTW, how to "unhide" them with a using-declaration.
what is syntax?

regards!

jesse
 
J

jesse

jesse said:
Why Compiler design this this way? i don't see any pitfall or particular
reason?

at least, in java, it seems no hiding existing.

BTW, how to "unhide" them with a using-declaration.
what is syntax?

regards!

jesse
 
J

Janusz Szpilewski

jesse said:
Why Compiler design this this way? i don't see any pitfall or particular
reason?

at least, in java, it seems no hiding existing.

BTW, how to "unhide" them with a using-declaration.
what is syntax?

If you override an overloaded member function all other overloaded ones
get hidden in the derived class context. It is intended to avoid some
problems with unexpected calls of member functions the author of the
overridden version might not be aware of. There are some issues with
implicit type promotion in C++ that may lead to unpredictable results in
some situations.

In your case you must explicitly scope the base member function:

Base::talk(false);

or "unhide" it when overriding like:

/...
using Base<int>::talk;
void talk(){ /* ... */ }
/...

Regards,
Janusz
 
D

DarkSpy

#include <string>
#include <iostream>

template <typename T>
class Base
{
public:
Base(){
};
~Base(){
};

void talk(){
std::cout << " this is base talk" << std::endl;
};

void talk(bool flag){
std::cout << " this is base talk with flag" << std::endl;
};
void sing(){
std::cout << " this is base sing" << std::endl;
};

};
template <typename T>
class Derived:public Base<T>
{
public:
Derived():Base<T>()
{

};
~Derived()
{
};
};
template <>
class Derived<int>:public Base<int>
{
public:
Derived():Base<int>()
{
talk(); // <--------------- Location A;
talk(false); // <--------------- Location B;
sing(); // <--------------- Location C;
};
~Derived()
{
};
void talk(){
std::cout << " this is derived talk" << std::endl;
};
};


int main(void)
{
Derived<int> m;
m.talk();
return 0;
}

when i compile it with g++, i get the following error:

/*
* mytest.cpp: In constructor `Derived<int>::Derived()':
* mytest.cpp:43: no matching function for call to
`Derived<int>::talk(bool)'
* mytest.cpp:49: candidates are: void Derived<int>::talk()
*/

I just wondering why in location C, the method sing() can be called
while talk(false) can not be called since both are in the base class.

when i compile it with MS VStudio. i get similar error:

Compiling...
mytest.cpp
D:\code\c++\TestGcc\mytest.cpp(43) : error C2660: 'talk' : function does
not take 1 parameters
Error executing cl.exe.

TestGcc.exe - 1 error(s), 0 warning(s)


is this a bug of implementation in compiler?
it's rule.
see C++ 98 part: 10.2: Member name lookup.

for your source code, explicit using Base<int>::talk(false); or
explicit using:
using Base<int>::talk;

that's will resolve your problem



[comp.lang.c++]
[comp.lang.c++.moderated]
DarkSpy, A C++ Mad Dog. :)
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top