How can I return a value in template function?

L

learning_C++

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 said:
::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;
}
 
G

Gernot Frisch

#include said:
#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?
 
S

Sumit Rajan

learning_C++ said:
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 said:
::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 said:
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.
 
S

Sam Holden

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> >&)
':

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:

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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top