473,405 Members | 2,154 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,405 software developers and data experts.

my Vector class

180 100+
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.

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class Vector
  3. {
  4.     int _size;
  5.     T* data;
  6.     char* charData;
  7. public:
  8.     Vector(int size = 0):_size(size),data(new T[_size])
  9.     {}
  10.     ~Vector()
  11.     {
  12.       delete[] data;
  13.     }    
  14.     friend istream& operator>>(istream& is, Vector<T>& v)
  15.     {
  16.         for(int i=0; i<v._size; ++i)
  17.         {
  18.         is>>v.data[i];
  19.         }
  20.         return is;
  21.     }
  22.     T& operator[](int index); 
  23.     const T& operator[](int index)const;
  24. };
  25. template<class T>
  26. T& Vector<T>::operator[](int index)
  27. {
  28.     return data[index];    
  29. }
  30.  
  31. template<class T>
  32. const T& Vector<T>::operator[](int index)const
  33. {
  34.     return data[index];    
  35. }
  36.  
  37. int main()
  38. {    
  39.     //class Vector works for int, char, double 
  40.     //but fails for string or char*
  41.     Vector<int> v(5);
  42.     cin>>v;
  43.     for(int i=0; i<5; ++i)
  44.         cout<<v[i]<<"\t";
  45.  
  46.     Vector<char* > v(5); //fails for this or string
  47.     cin>>v;
  48.     for(int i=0; i<5; ++i)
  49.         cout<<v[i]<<"\t";
  50.  
  51.     return 0;
  52. }
Oct 31 '06 #1
2 2339
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:
Expand|Select|Wrap|Line Numbers
  1.    43  int main()
  2.     44  {
  3.     45          //class Vector works for int, char, double 
  4.     46          //but fails for string or char*
  5.     47          Vector<int> v(5);
  6.     48          cout<<" Enter 5 elements \n";
  7.     49          cin>>v;
  8.     50          for(int i=0; i<5; ++i)
  9.     51                  cout<<v[i]<<"\t";
  10.     52  //----SEE this
  11.     53          Vector<std::string > s(5); 
  12.     54          cin>>s;
  13.     55          for(int i=0; i<5; ++i)
  14.     56                  cout<<s[i]<<"\t";
  15.     57         
  16.     58          char * c_ptr = new char[8];
  17.     59          c_ptr[0] = 'a'; 
  18.     60          c_ptr[1] = 't'; 
  19.     61          c_ptr[2] = 'g'; 
  20.     62          c_ptr[3] = 'e'; 
  21.     63          c_ptr[4] = 'f'; 
  22.     64          c_ptr[5] = 'k'; 
  23.     65          c_ptr[6] = 'j'; 
  24.     66          c_ptr[7] = 'z'; 
  25.     67
  26.     68          Vector<char *> c(5); 
  27.     69          for(int i=0; i<5; ++i)
  28.     70                  c[i]=c_ptr+i;
  29.     71          for(int i=0; i<5; ++i)
  30.     72                  cout<<*c[i]<<"\t";
  31.     73         
  32.     74          return 0;
  33.     75  } 
  34.     76
  35.     77
Oct 31 '06 #2
vermarajeev
180 100+
Hi sorry,
I was a fool not to see that.

Thankx
Oct 31 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alan Benn | last post by:
(VC6) When I use the STL <vector> template as follows: #include <vector> .... vector<CString> m_nameList; // Names of the chips I get these compiler warnings : C:\Program...
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: helge | last post by:
What is the best way to implement a vector in space R3, i.e a vector holding three floats, supporting arithmetic operations, dot and cross product etc in c++? is there a standard library class for...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.