By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

How can I return a value in template function?

P: n/a
I hope to use template. in my code I hope to sum the vector<int> and
vector<double>. But there are several errors:

sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no
type
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
':
sumtemplate.cpp:7: warning: `std::vector<T, std::allocator<_CharT>
::iterator'

is implicitly a typename
sumtemplate.cpp:7: warning: implicit typename is deprecated, please
see the
documentation for details
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
[with T = double]':
sumtemplate.cpp:27: instantiated from here
sumtemplate.cpp:11: warning: return to `int' from `double'
sumtemplate.cpp:11: warning: argument to `int' from `double'

Please help me.

#include<iostream>
#include<vector>

using namespace std;

template <class T> sum(vector<T> &v){
vector<T>::iterator iter;
T result;
for(iter=v.begin();iter<v.end();iter++)
result=result+*iter;
return result;
}

int main(){
vector<int> v1;
vector<double> v2;
int sum1;
double sum2;

v1.push_back(1);
v1.push_back(2);
sum1=sum(v1);
cout<<"The result(int) is: "<<sum1<<endl;

v2.push_back(1.1);
v2.push_back(2.2);
sum2=sum(v2);
cout<<"The result(double) is: "<<sum2<<endl;
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> #include<iostream>
#include<vector>

using namespace std;

template <class T> sum(vector<T> &v){


should be:
template <class T> T sum(vector<T> &v){
You're returning a 'T', aren't you?
Jul 22 '05 #2

P: n/a
On 14 Sep 2004 06:02:53 -0700, learning_C++ <le********@hotmail.com> wrote:
I hope to use template. in my code I hope to sum the vector<int> and
vector<double>. But there are several errors:

sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no
type
sum() needs a return type.

template <class T> T sum(vector<T> &v) {

seems the appropriate type.

sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
':
sumtemplate.cpp:7: warning: `std::vector<T, std::allocator<_CharT>
::iterator' is implicitly a typename
sumtemplate.cpp:7: warning: implicit typename is deprecated, please
see the
documentation for details


Check the documentation I guess...

The compiler doesn't know that vector<T>::iterator is a type, so
you need to tell it such:

typename vector<T>::iterator iter;

sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
[with T = double]':
sumtemplate.cpp:27: instantiated from here
sumtemplate.cpp:11: warning: return to `int' from `double'
sumtemplate.cpp:11: warning: argument to `int' from `double'
You compiler has used C's implicit int rule to determine that
sum() returns an int, this of causes problems when you try
and return a double. It'll go away when you fix the error
above.

Please help me.
The changes in place are:
#include<iostream>
#include<vector>

using namespace std;

template <class T> sum(vector<T> &v){
vector<T>::iterator iter;
The above two lines need changing, as described above.
T result;


This doesn't initialise result and hence you'll get garbage.
--
Sam Holden
Jul 22 '05 #3

P: n/a

"learning_C++" <le********@hotmail.com> wrote in message
news:44**************************@posting.google.c om...
I hope to use template. in my code I hope to sum the vector<int> and
vector<double>. But there are several errors:

sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no
type
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
':
sumtemplate.cpp:7: warning: `std::vector<T, std::allocator<_CharT>
::iterator' is implicitly a typename
sumtemplate.cpp:7: warning: implicit typename is deprecated, please
see the
documentation for details
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
[with T = double]':
sumtemplate.cpp:27: instantiated from here
sumtemplate.cpp:11: warning: return to `int' from `double'
sumtemplate.cpp:11: warning: argument to `int' from `double'

Please help me.

#include<iostream>
#include<vector>

using namespace std;

template <class T> sum(vector<T> &v){


template <class T> T sum(vector<T> &v){
vector<T>::iterator iter;
typename vector<T>::iterator iter;
"iterator" is a type.

T result;
T result = T();
You need to initialize result to 0.
for(iter=v.begin();iter<v.end();iter++)
result=result+*iter;
return result;
}
So, your function, sum, now looks like:

template <class T>
T sum(vector<T> &v)
{
typename vector<T>::iterator iter;
T result=T();
for(iter=v.begin();iter<v.end();iter++)
result=result+ (*iter);
return result;
}

Regards,
Sumit.

int main(){
vector<int> v1;
vector<double> v2;
int sum1;
double sum2;

v1.push_back(1);
v1.push_back(2);
sum1=sum(v1);
cout<<"The result(int) is: "<<sum1<<endl;

v2.push_back(1.1);
v2.push_back(2.2);
sum2=sum(v2);
cout<<"The result(double) is: "<<sum2<<endl;
return 0;
}


Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.