473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

About the instantiation of a template class

Hi, folks,
I'm running into a question as below:

template<typename T>
class A {
private:
T _a;
public:
A(T t): _a(t) {
}
};

template<typename T>
void operator+(A<Tlhs, A<Trhs) {
}

int main() {
A<intobj(5);
int t = 5;
obj + t;
}

When I try to compile the code above under MSVC8, I got the following
errors:

1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'

2. error C2676: binary '+' : 'A<T>' does not define this operator or a
conversion to a type acceptable to the predefined operator
with
[
T=int
]

Any informative reply will be appreciated, thanks in advance.

Best regards,
Liu Hao

Oct 12 '07 #1
3 1560
Hi Liu Hao,
void operator+(A<Tlhs, A<Trhs) {
This function takes two objects of the A class.
obj + t;
But here you're calling operator+ with one A object and one int.
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'
The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobject.

If you used two A objects it would work:

A<intobj(5);
A<intobj2(5);
obj + obj2;

You could also rewrite your + operator so that the object doesn't need
to be an instance of A<something>:

void operator+(A<Tlhs, T rhs)

Or alternatively it may be possible to define an operator that allows an
int to be converted into an A<int- sorry, I'm not sure of the exact
syntax. (I would've thought the constructor would be called implicitly
though - perhaps someone can enlighten us.)

Cheers,
Adam.
Oct 12 '07 #2
On 10 12 , 10 31 , Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
Hi Liu Hao,
void operator+(A<Tlhs, A<Trhs) {

This function takes two objects of the A class.
obj + t;

But here you're calling operator+ with one A object and one int.
>
>I think here the compiler is invoking the constructor A(int t) of class A<int>, thus got an temporary A<intobject.
>
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'

The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobject.

If you used two A objects it would work:

A<intobj(5);
A<intobj2(5);
obj + obj2;

You could also rewrite your + operator so that the object doesn't need
to be an instance of A<something>:

void operator+(A<Tlhs, T rhs)

Or alternatively it may be possible to define an operator that allows an
int to be converted into an A<int- sorry, I'm not sure of the exact
syntax. (I would've thought the constructor would be called implicitly
though - perhaps someone can enlighten us.)

Cheers,
Adam.

Oct 12 '07 #3
On Oct 12, 4:31 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
void operator+(A<Tlhs, A<Trhs) {
This function takes two objects of the A class.
This is not a function, but a template. It only becomes a
function when instantiated.
obj + t;
But here you're calling operator+ with one A object and one int.
Here, you're asking the compiler to deduce the types for the
above template, and instantiate it using the deduced types.
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'
The compiler is complaining that it doesn't know how to convert an int
into an A<somethingobject.
No. The compiler knows how to convert an int into an A<int>
object, which is what is wanted. The problem here is that the
rules for type deduction do not allow the compiler to deduce the
arguments for the operator+ template, so it cannot instantiate
the function.
If you used two A objects it would work:
A<intobj(5);
A<intobj2(5);
obj + obj2;
You could also rewrite your + operator so that the object
doesn't need to be an instance of A<something>:
void operator+(A<Tlhs, T rhs)
Or alternatively it may be possible to define an operator that
allows an int to be converted into an A<int- sorry, I'm not
sure of the exact syntax. (I would've thought the constructor
would be called implicitly though - perhaps someone can
enlighten us.)
The usual solution here would be something along the lines of:

template< typename T >
class A
: public Operators< A< T
, public MixedOperators< A< T >, T >
{
public:
// ...
A& operator+=( A const& other ) ;
} ;

with the usual definitions for Operators and MixedOperators:

template< typename T >
class Operators
{
public:
friend T operator+( T const& lhs, T const& rhs )
{
T result( lhs ) ;
result += rhs ;
return result ;
}
// And so on for the other operators...
} ;

template< typename T1, typename T2 >
class MixedOperators
{
public:
friend T1 operator+( T1 const& lhs, T2 const& rhs )
{
T1 result( lhs ) ;
result += rhs ; // Note that here, template
// type deduction isn't needed,
// so the compiler will find
// any necessary conversions.
return result ;
}
// And so on for the other operators...
friend T1 operator+( T2 const& lhs, T1 const& rhs )
{
T1 result( rhs ) ;
result += lhs ;
return result ;
}
// And so on for the other commutative operators...
} ;

(Note that in this solution, there are no function
templates:-).)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 12 '07 #4

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

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
3
by: erictham115 | last post by:
Error C2555 c:\C++ projects\stat1\stdmatrix_adapt.h(41) : error C2555: 'std_tools::Matrix_adapter<T>::at': overriding virtual function return type differs and is not covariant from...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
1
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear...
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.