Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a template class and a template function that works with instances of the template class:
C++
template <class t> class calc
{
public:
    T el;
    calc(T t);
.............
};
template<class t> calc<t>::calc(T t){el=t;}


C++
template<class tt1, class tt2> double the_sum(calc<int> &a,calc<double> &b)
{
    return a.el+b.el; 
}

C++
void main()
{
        calc<int> C(10);
	calc<double> D(5.2);
	cout<<the_sum(C,D);/*->this is where I get  2 errors:
	1	error C2783: 'double the_sum(calc<t>,calc<double>)' : could not deduce template argument for 'TT1'	
	2	error C2783: 'double the_sum(calc<t>,calc<double>)' : could not deduce template argument for 'TT2'	
*/	


}

What is wrong with my function and how shoul I call the function "the_sum" in the main program, to get the expected answer. is it possible?
Thanks in advance!
Posted
Updated 3-Aug-11 1:56am
v3
Comments
Philippe Mori 2-Aug-11 20:58pm    
What are the quoted in class T="". T is a type so it does not make sence to set a default with a value. class T = double would make sense if you wnat to uses double if not specified for example.
Alex_RO 3-Aug-11 4:34am    
I have written just T , and at the function I have written both TT1 and TT2 but I don't know why it has appeared T="". thanks,
Philippe Mori 3-Aug-11 7:32am    
You must write &lt; instead of < and &gt; instead of > as otherwise Code Project editor box will interpret it as XML and it seems that it will quote attributes values. It will also add a bunch of closing tags at the end.
Philippe Mori 3-Aug-11 7:40am    
I have tried to fix it but I am not sure of the original code for the second code snippet as for sure it won't compile without explicit specification of tt1 and tt2 type as it does not appears in the argument list. Maybe you can "improve question" to correct any error.
Alex_RO 3-Aug-11 8:03am    
Yes,your fix is excellent. This is how I intendet to look like my original code. Thank a lot for the fix.

The compiler doesn't know what type to use for tt1 and tt2 for instanciation therefore the message.

template <class tt1, class tt2> double the_sum(tt1& a,tt2& b)
{
 return (double)(a.el+b.el);
}

In this case the compiler can instanciate the template function for the types calc<int> and calc<double> implicit.
This maybe look better, regards.
 
Share this answer
 
v2
Comments
Alex_RO 2-Aug-11 10:30am    
Thanks both, _Superman_ and mbue. I have realized the error,it was a negligence of mine. Both solutions work, but what I wanted to do in fact, a template function, is what mbue said.
Philippe Mori 2-Aug-11 21:27pm    
Well a template like that would works (after correcting typos) but if you want it to only accept calc<T> objects, then my solution is better.
mbue 3-Aug-11 4:11am    
Maybe ;). My template function works for every class that has a el member therfore there is more flexibility.
Regards.
Philippe Mori 3-Aug-11 7:42am    
I have tried to fix < vs &lt stuff. Hope I have get it right. If not, then improve the question.
mbue 3-Aug-11 12:38pm    
Thanks. Formatting is a little bit tricky. ;)
Based on what I understand, this is what I've come up with -
XML
template <class T>
class calc
{
public:
    T el;
    calc(T t);
};

template<class T>
calc<T>::calc(T t)
{
    el = t;
}

double the_sum(calc<int> &a, calc<double> &b)
{
    return a.el + b.el;
}
 
Share this answer
 
Comments
Philippe Mori 2-Aug-11 21:23pm    
Your function the_sum is not generic anymore. Your must have a calc<int> and a calc<double> in that order.
C++
template <class T = double>   // default to type double
class calc
{
public:
    T el;
    calc(T t);
};

template<class T>
calc<T>::calc(T t)
{
    el = t;
}

// By using a more precise type as the argument, the compiler can verify that
// the function is called on an object of type calc<T>.

// Here const is not strictly required... but since the value is not modified
// it make sence to do so and it will allows const & objects to be passed.

template <class R, class S> double the_sum(calc<R> const &a, calc<S> const & b)
{
    return a.el + b.el;
}

int main()
{
    calc<int> c(2);
    calc<> d(2.3);    // double can be ommited here since default
    the_sum(c, d);

    return 0;
}


This code was tested in Visual C++ 2010.
 
Share this answer
 
My above solution can be improved for automatic return type deduction on a recent compiler:

C++
template <class R, class S>
auto the_sum(calc<R> const &a, calc<S> const & b) -> decltype (a.el + b.el)
{
    return a.el + b.el;
}
 
Share this answer
 
Comments
Alex_RO 3-Aug-11 5:04am    
I know what you mean,also found something on the internet, but all are compiled with VS2010 and under my VS2008 this code does not work, possible to be because of the oldest type of compiler? I'm going to install VS2010.
Philippe Mori 3-Aug-11 7:49am    
With Visual Studio 2008 and boost you can have something in between (see libraries that have Function in their names: Boost 1.47.0 Library Documentation).

But using Visual Studio 2010 is far better is you want to do advanced C++ as it far more up-to-date with latest standards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900