Overloading Operators
Feb 24, 2012 at 12:17am UTC
So I'm working on an assignment regarding templates. We're to inherit an array class we created to make a fixed size array. The only problem I'm having is with the assignment operator. I have two operators because the class is designed to allow two arrays of differing size to be set equal to each other (through resizing).
Operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//
// operator =
//
template <typename T, size_t N>
const Fixed_Array <T, N> & Fixed_Array <T, N>::operator = (const Fixed_Array <T, N> & rhs)
{
for (size_t x = 0; x < N; x++)
{
this ->set(x, rhs[x]);
}
}
//
// operator =
//
template <typename T, size_t N>
template <size_t M>
const Fixed_Array <T, N> & Fixed_Array <T, N>::operator = (const Fixed_Array <T, M> & rhs)
{
this ->resize(M);
for (size_t x = 0; x < M; x++)
{
this ->set(x, rhs[x]);
}
}
So when I try to use it using this code:
1 2 3
Fixed_Array<char , 10> * fa1 = new Fixed_Array<char , 10>();
Fixed_Array<char , 20> * fa2 = new Fixed_Array<char , 20>();
fa1 = fa2;
I get this error:
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\cs507hw\hw2\driver.cpp(16): error C2440: '=' : cannot convert from 'Fixed_Array<T,N> *' to 'Fixed_Array<T,N> *'
1> with
1> [
1> T=char,
1> N=20
1> ]
1> and
1> [
1> T=char,
1> N=10
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
any suggestions?
Last edited on Feb 24, 2012 at 2:56am UTC
Topic archived. No new replies allowed.