* temlate class
@ 2004-09-14 9:01 learning c++
2004-09-14 9:54 ` Claudio Bley
0 siblings, 1 reply; 2+ messages in thread
From: learning c++ @ 2004-09-14 9:01 UTC (permalink / raw)
To: gcc-help
Hi,
I compÃled a template class. I hope it can apply to int and double. Also, I
hope to use vector class in my code. But it does not work.
Please help me.
#include <vector>
#include <iostream>
using namespace std;
template <class T> class vector1{
int len;
//double mean;
T sum;
public:
//double mean();
T vector_sum (vector(T) &vec);
};
template <>
T vector1<T>::vector_sum(vector(T) &vec){
vector<T>::iterator iter;
for(iter=vec.begin();iter<vec.end();iter++)
sum=sum+*iter;
return sum;
}
int main(){
int sum;
double sum2;
vector1<int> v1;
vector1<double> v2;
v1.push_back(1);
v1.push_back(2);
sum=vector1::vector_sum(v1);
cout << "the result is: "<< sum<<endl;
v2.push_back(1.1);
v2.push_back(1.2);
sum2=vector1::vector_sum(v2);
return 0;
}
_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: temlate class
2004-09-14 9:01 temlate class learning c++
@ 2004-09-14 9:54 ` Claudio Bley
0 siblings, 0 replies; 2+ messages in thread
From: Claudio Bley @ 2004-09-14 9:54 UTC (permalink / raw)
To: gcc-help
On Tue, Sep 14, 2004 at 11:50:49AM +0300, learning c++ wrote:
> Hi,
> I comp?led a template class. I hope it can apply to int and double. Also, I
> hope to use vector class in my code. But it does not work.
> Please help me.
> T vector1<T>::vector_sum(vector(T) &vec){
Again, a C++ programming question -- off topic here.
And you should at least show that you've taken some effort to
solve the problem yourself before posting a question to a forum.
You not even got the syntax right???
It should read "vector<T>" instead of "vector(T)".
Please get a book and read it through. Stop bugging people
on this mailing list.
(I haven't taken my sedatives today...)
--
Claudio
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-09-14 9:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-14 9:01 temlate class learning c++
2004-09-14 9:54 ` Claudio Bley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).