temlate class

learning c++ learning_c@hotmail.com
Tue Sep 14 09:01:00 GMT 2004


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



More information about the Gcc-help mailing list