Template class operators overloading difficulty

  • Thread starter Theodore V. Tolstoy
  • Start date
T

Theodore V. Tolstoy

Hi!

GCC compiler produces:

test2.cc: In function `int main()':
test2.cc:17: no match for `CTest<char, 1>& + CTest<char, 2>&' operator

when compiling text:

01 template<class T, int S>
02 class CTest
03 {
04 };
05
06 template<class T, int S1, int S2, int S3>
07 CTest<T, S3> operator+ ( const CTest<T, S1>& a, const CTest<T, S2>& b )
08 {
09 }
10
11 int main()
12 {
13 CTest<char, 1> t1;
14 CTest<char, 2> t2;
15 CTest<char, 3> t3;
16
17 t3 = t1 + t2;
18 }

Any comment?
 
J

John Harrison

Theodore V. Tolstoy said:
Hi!

GCC compiler produces:

test2.cc: In function `int main()':
test2.cc:17: no match for `CTest<char, 1>& + CTest<char, 2>&' operator

when compiling text:

01 template<class T, int S>
02 class CTest
03 {
04 };
05
06 template<class T, int S1, int S2, int S3>
07 CTest<T, S3> operator+ ( const CTest<T, S1>& a, const CTest<T, S2>&
b )
08 {
09 }
10
11 int main()
12 {
13 CTest<char, 1> t1;
14 CTest<char, 2> t2;
15 CTest<char, 3> t3;
16
17 t3 = t1 + t2;
18 }

Any comment?

Yes, the compiler cannot deduce the S3 template parameter because return
types are not considered when doing template function argument deduction.

john
 
R

Rob Williscroft

Theodore V. Tolstoy wrote in @posting.google.com in comp.lang.c++:
Any comment?

Yep, please post without the line no's, it hinders cut and paste :).

Anyway, John Harrison has already told you the problem, here's
a possible solution:

#include <iostream>

template < typename T, int S >
struct CTest;

class CTest_ops
{
template < typename T, int S >
friend struct CTest;

template< typename T, int S1, int S2 >
struct operator_plus
{
CTest<T, S1> const & a;
CTest<T, S2> const & b;
operator_plus( CTest<T, S1> const &aa, CTest<T, S2> const &bb ) :
a( aa ), b( bb )
{
}
};

template < typename T, int S1, int S2 >
friend CTest_ops::eek:perator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b );
};


template < typename T, int S1, int S2 >
CTest_ops::eek:perator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b )
{
return CTest_ops::eek:perator_plus< T, S1, S2 >( a, b );
}


template < typename T, int S >
struct CTest
{
T data;

CTest( T const &a ) : data( a ) {}
CTest() {}

template < int S2, int S3 >
CTest operator = ( CTest_ops::eek:perator_plus< T, S2, S3 > const &p )
{
data = p.a.data + p.b.data;
return *this;
}
};


int main()
{
CTest< int, 1> t1( 1 );
CTest< int, 2> t2( 2 );
CTest< int, 3> t3;

t3 = t1 + t2;

std::cout << t3.data << '\n';
}

Rob.
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top