my Vector class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    my Vector class

    I'm unable to use char* or string in Vector class.
    I know what the problem is but unable to find a proper answer.

    One solution is like I can call explicit intialization for char* or string
    and perform operations for only those but unable to get how to do that.

    Can someone help please.
    I want Vector to work with any datatype.

    Code:
    template<class T>
    class Vector
    {
    	int _size;
    	T* data;
    	char* charData;
    public:
    	Vector(int size = 0):_size(size),data(new T[_size])
    	{}
    	~Vector()
    	{
    	  delete[] data;
    	}	
    	friend istream& operator>>(istream& is, Vector<T>& v)
    	{
    		for(int i=0; i<v._size; ++i)
    		{
    		is>>v.data[i];
    		}
    		return is;
    	}
    	T& operator[](int index); 
    	const T& operator[](int index)const;
    };
    template<class T>
    T& Vector<T>::operator[](int index)
    {
    	return data[index];	
    }
    
    template<class T>
    const T& Vector<T>::operator[](int index)const
    {
    	return data[index];	
    }
    
    int main()
    {	
    	//class Vector works for int, char, double 
    	//but fails for string or char*
    	Vector<int> v(5);
    	cin>>v;
    	for(int i=0; i<5; ++i)
    		cout<<v[i]<<"\t";
    
    	Vector<char* > v(5); //fails for this or string
    	cin>>v;
    	for(int i=0; i<5; ++i)
    		cout<<v[i]<<"\t";
    
    	return 0;
    }
  • Jai Vrat Singh
    New Member
    • Oct 2006
    • 18

    #2
    It should not fail, You are doing a major mistake.. How can you take a char pointer argument from stdin ????? Do you know address where chars are stored..

    here it works
    [HTML][jsingh]:/home/jsingh/cpp/templates/test% ./a.out
    Enter 5 elements
    1 2 3 4 5
    1 2 3 4 5
    aa bb cc dd eee fff ggg
    aa bb cc dd eee a t g e f [jsingh@sgs45a-0145]:/home/jsingh/cpp/templates/test% [/HTML]

    Code is same:
    Code:
       43  int main()
        44  {
        45          //class Vector works for int, char, double 
        46          //but fails for string or char*
        47          Vector<int> v(5);
        48          cout<<" Enter 5 elements \n";
        49          cin>>v;
        50          for(int i=0; i<5; ++i)
        51                  cout<<v[i]<<"\t";
        52  //----SEE this
        53          Vector<std::string > s(5); 
        54          cin>>s;
        55          for(int i=0; i<5; ++i)
        56                  cout<<s[i]<<"\t";
        57         
        58          char * c_ptr = new char[8];
        59          c_ptr[0] = 'a'; 
        60          c_ptr[1] = 't'; 
        61          c_ptr[2] = 'g'; 
        62          c_ptr[3] = 'e'; 
        63          c_ptr[4] = 'f'; 
        64          c_ptr[5] = 'k'; 
        65          c_ptr[6] = 'j'; 
        66          c_ptr[7] = 'z'; 
        67
        68          Vector<char *> c(5); 
        69          for(int i=0; i<5; ++i)
        70                  c[i]=c_ptr+i;
        71          for(int i=0; i<5; ++i)
        72                  cout<<*c[i]<<"\t";
        73         
        74          return 0;
        75  } 
        76
        77

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Hi sorry,
      I was a fool not to see that.

      Thankx

      Comment

      Working...