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