473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template class operators overloading difficulty

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?

---
SY, TVT
Jul 22 '05 #1
2 1410

"Theodore V. Tolstoy" <tv*@bk.ru> wrote in message
news:7b**************************@posting.google.c om...
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
Jul 22 '05 #2
Theodore V. Tolstoy wrote in news:7bec5a7a.0410032226.309b1366
@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::operator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b );
};
template < typename T, int S1, int S2 >
CTest_ops::operator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b )
{
return CTest_ops::operator_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::operator_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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

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

Similar topics

12
1866
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
5
1683
by: Steven T. Hatton | last post by:
The comperable operator*() to that shown here works for a non-template class: template <typename T> inline Vector3<T> operator*(const Vector3<T>& v, const T& n) { Vector3<T> t(v); return t *=...
9
2292
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
2825
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
1
2112
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
5
2085
by: Andrei Tarassov | last post by:
Hi! I would like to know if it is possible to implement something like this: template <class X, class Y> X void func(X, Y) { ... } template <class X, class Y> Y void func(X, Y) { ... }
7
2078
by: AlanJSmith | last post by:
I am converting some C++ projects from 6 to vs2005 in one of the headers i have the code that follows this passage in which I get an error saying int default cant be assumed so i added...
1
2743
by: amhoov | last post by:
Hi all, OS - MacOS X 10.4.9 Compiler - gcc 4.0 (XCode) I'm *extremely* new to C++ (but have extensive experience with Java), so please go easy on me. I'm running into a very odd situation...
3
1565
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I'm running into a question as below: template<typename T> class A { private: T _a; public: A(T t): _a(t) { }
0
7199
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
7273
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
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
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
7451
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...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.