473,396 Members | 1,996 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,396 software developers and data experts.

problem with templates and inheritance

Hello group,

I've just stumbled upon a compile-error which I don't understand. I was
hoping someone can explain to me why my code is a problem.

I'm implementing a somewhat complex class hierarchy for a hobby project,
and I need to repeat it with several value types. So, I chose to
express it with templates. Here is sample code that demonstrates the
problem:

// code start
template <typename T>
class base {
T m_val;
public:
base(T init) : m_val(init) {}
T val() { return m_val; }
};

template <typename T>
class derived : public base<T> {
public:
derived(T init) : base<T>(init) {}

// adding this causes compile error:
T val(T x) { return x; }

// and this fixes it again
// T val() { return base<T>::val(); }
};

int main(void) {
derived<int> d(1);
int i=d.val();
return 0;
}
// code end

When I try to invoke val() (the no-args variant defined in the base
class base<T>) on a derived<T> object, and derived<T> defines a val(T x)
(with args). Removing this val(T x) from derived<T> also removes the
compile error. Compile error looks like this:

t.cc: In function `int main()':
t.cc:24: error: no matching function for call to `derived<int>::val()'
t.cc:16: note: candidates are: T derived<T>::val(T) [with T = int]

I'm using g++ 3.4.1.

Thanks
Ivan
Aug 27 '05 #1
2 1257
Ivan Riis Nielsen wrote:

//*adding*this*causes*compile*error:
T*val(T*x)*{*return*x;*}


http://www.parashift.com/c++-faq-lit....html#faq-23.7

Max
Aug 27 '05 #2
Max M. wrote:
Ivan Riis Nielsen wrote:
// adding this causes compile error:
T val(T x) { return x; }

http://www.parashift.com/c++-faq-lit....html#faq-23.7

Max


Thanks a lot for the pointer. So, it wasn't template related at all...
In my simplistic mind, anything defined public in a public base classes
would carry the same weight as things defined locally, and then the
compiler could just choose the one which best matches the actual
argument given in a call. But the "using" trick is a quite small
effort, and a new word in my vocabulary:-)

Ivan
Aug 27 '05 #3

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

Similar topics

1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
1
by: Michael | last post by:
Hello, I'm trying to implement sample I found on page templates, so I do not have to maintain "<html><head>..." on all my documents. But don't let that confuse you, this is an inheritance...
1
by: David Berman | last post by:
Hello, I'm having a problem using MapPath. The problem is that my production site is www.mydomain.com, but my development site is www.devdomain.com/myproject. In the root folder I have a base...
4
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
4
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.