473,396 Members | 1,853 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,396 software developers and data experts.

How can I return a value in template function?

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
3 2075
> #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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Damon | last post by:
Hi, Created the template function below but the g++ compiler keeps complaining "no matching function for call to....". Is there a way to make it work? Thank you. ==========snip part of...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
6
by: PLM | last post by:
Hello, all. I should lke to declare template function as following: template <class TYPE> TYPE Test() {return (TYPE)5; } void main(void) { int i; float f; i = Func(); f = Func();
32
by: Panks | last post by:
Hey All Its one of basic questions? i read in a book "Thinking in C++" it says " Overloading solely on return value is a bit too subtle, and thus isn't allowed in C++." on page 323. While i...
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
2
by: psujkov | last post by:
Hi everybody, let us have some class A with const static int variable var with compile-time well-known value. let us have some function f(), which has return type of A. and let us have some...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
5
by: C++Liliput | last post by:
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main()
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.