Inherited Template Overloading Problems

C

crjjrc

Hi. I'm trying to create a template class hierarchy and I'm running
into some trouble. I've worked up as small an example as I can that
reproduces the problem and placed it here:

http://www.cs.utk.edu/~cjohnson/forothers/example.txt

In this contrived example, the parent template class FixedVector<T,
len> defines every member function I need. I also create a derived
template class FixedVector2D<T> which is FixedVector<T, 2>. (Later on
I will give FixedVector2D functionality that FixedVector won't have,
thus two classes.) I also have a non-member binary addition function
that operates on and returns FixedVector<T, len> vectors. This is not
a friend function since it uses += and needs no access to any private
members.

Now, say I have three FixedVector<int, 5> vectors a, b, and c. I can
add two of them like so without compiler errors:

c = a + b;

But if I have three FixedVector2D<int> vectors d, e, and f, I get
compiler errors with:

f = d + e;

The compile errors don't wrap nicely:

small.cpp: In function 'int main(int, char**)':
small.cpp:75: error: no match for 'operator=' in 'f = operator+ [with
T = int, int len = 2](((const FixedVector<int, 2>&)((const
FixedVector<int, 2>*)(& d.FixedVector2D<int>::<anonymous>))), ((const
FixedVector<int, 2>&)((const FixedVector<int, 2>*)(&
e.FixedVector2D<int>::<anonymous>))))'
small.cpp:55: note: candidates are: FixedVector2D<int>&
FixedVector2D<int>::eek:perator=(const FixedVector2D<int>&)

It doesn't seem to matter if I define operator= explicitly, either in
just the parent or in both template classes. I get similar
compilation errors no matter what. What magic trick am I missing to
get this working properly? What C++ caveat am I overlooking? Do I
need to define operator+ for the each subclass to?

I appreciate any help!

- Chris
 
V

Victor Bazarov

crjjrc said:
Hi. I'm trying to create a template class hierarchy and I'm running
into some trouble. I've worked up as small an example as I can that
reproduces the problem and placed it here:

http://www.cs.utk.edu/~cjohnson/forothers/example.txt

In this contrived example, the parent template class FixedVector<T,
len> defines every member function I need. I also create a derived
template class FixedVector2D<T> which is FixedVector<T, 2>. (Later on
I will give FixedVector2D functionality that FixedVector won't have,
thus two classes.) I also have a non-member binary addition function
that operates on and returns FixedVector<T, len> vectors. This is not
a friend function since it uses += and needs no access to any private
members.

Now, say I have three FixedVector<int, 5> vectors a, b, and c. I can
add two of them like so without compiler errors:

c = a + b;

But if I have three FixedVector2D<int> vectors d, e, and f, I get
compiler errors with:

f = d + e;

The compile errors don't wrap nicely:

small.cpp: In function 'int main(int, char**)':
small.cpp:75: error: no match for 'operator=' in 'f = operator+ [with
T = int, int len = 2](((const FixedVector<int, 2>&)((const
FixedVector<int, 2>*)(& d.FixedVector2D<int>::<anonymous>))), ((const
FixedVector<int, 2>&)((const FixedVector<int, 2>*)(&
e.FixedVector2D<int>::<anonymous>))))'
small.cpp:55: note: candidates are: FixedVector2D<int>&
FixedVector2D<int>::eek:perator=(const FixedVector2D<int>&)

It doesn't seem to matter if I define operator= explicitly, either in
just the parent or in both template classes. I get similar
compilation errors no matter what. What magic trick am I missing to
get this working properly? What C++ caveat am I overlooking? Do I
need to define operator+ for the each subclass to?

You need to add

FixedVector2d& operator=(FixedVector<T,2> const&);

to your derived class. Make sure to implement it properly.

There was a discussion here recently about "virtual operator+" or some
such. Dig it up and read. Use "Google groups" archives.

V
 
C

crjjrc

You need to add

FixedVector2d& operator=(FixedVector<T,2> const&);

to your derived class. Make sure to implement it properly.

There was a discussion here recently about "virtual operator+" or some
such. Dig it up and read. Use "Google groups" archives.

Ahh, that worked beautifully! Thanks so much!

- Chris
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top