Andrei Popescu
Andrei Popescu

Reputation: 25

Template Vector in C++

I've been having issues with using templates for a vector class. For some reason marime (size) is getting received as a pointer. I've managed to relieve the code of errors. Some help would be welcomed.

#include <iostream>
#include <stdio.h>

using namespace std;

template <class T>
class Vector {
      private: int marime; T *vect;
      public: Vector (int marime = 0){vect = new T[marime];}
              ~Vector (){
                      if ( vect != NULL ) delete [] vect;
              }
              int getMarime();
              T getVect(int);
              void setMarime(int);
              void setVect(T,int);
              Vector <T> operator+ (Vector);
              Vector <T> operator= (Vector);
              template <class U> friend istream & operator >>(istream, Vector); 
              template <class U> friend ostream & operator <<(ostream, Vector); 
};

template <class T > 
istream & operator>> (istream & in, Vector<T> & v)
{
        T val;
        int marime = v.getMarime();
        for (int i=0;i<marime; i++) {
            in>>val;
            v.setVect(val,i);
            }
        return in;
}

template <class  T> 
ostream & operator<< (ostream& out, Vector<T> & v)
{
        for (int i=0;i<v.getMarime(); i++) 
            out<<v.getVect(i)<< " " ;
        return out; 
}

template <class T>
int Vector<T>::getMarime(){
   return marime;  
     }

template <class T>
T Vector<T>::getVect(int i){
   return vect[i];
}

template <class T>
void Vector<T>::setMarime(int n){
     this->marime = n;
}

template <class T>
void Vector<T>::setVect(T val, int i){
     this->vect[i] = val;
}

template <class T>
Vector<T> Vector<T>::operator +(Vector<T> vector){
          Vector<T> temp(marime + vector.marime);
          for (int i=0; i < marime; i++)
              temp.vect[i] = vect[i];
          for (int i=marime+1; i<temp.marime; i++)
              temp.vect[i] = vector.vect[i];
          return *temp;
}

template <class T>
Vector<T> Vector<T>::operator= (Vector<T> vector){
          Vector<T> temp(vector.marime);
          for (int i=0; i < vector.marime; i++) temp.vect[i] = vector.vect[i];
          return *temp;
}
int main() {

 int n=3;
 Vector <int> A(n);
 cin>>A;
 cout<<A;   
 return 0;   

}

Upvotes: 0

Views: 2181

Answers (2)

juanchopanza
juanchopanza

Reputation: 227418

You are failing to initialize marime. You need to do this:

 public: Vector (int marime = 0) : marime(marime) {vect = new T[marime];}

otherwise it just contains an essentially random value.

To avoid confusion it is probably better not to use the name of the member variable as a constructor parameter name. It is quite common to use naming conventions for member variables, such as starting with m_, or finishing with an underscore (marime_).

Upvotes: 2

BlueWanderer
BlueWanderer

Reputation: 2691

public: Vector (int marime = 0){vect = new T[marime];}

Shouldn't there be a this->marine = marime?

Upvotes: 1

Related Questions