Simple Vector problem
Mar 5, 2014 at 12:36pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <iostream>
using namespace std;
int size = 10;
template <class T> class vector {
T* v;
public :
vector()
{
v = new T[size];
}
vector(T* a)
{
v = new T[size];
for (int i=0; i<size;i++)
v[i]=a[i];
}
friend ostream &operator << (ostream &o, vector t)
{
for (int i=0; i<size; i++)
o<<t.v[i]<<endl;
return o;
}
};
int main()
{
int x[10] = {1,2,3,4,5,6,7,8,9,10};
vector <int > v1;
v1=x;
cout<<v1;
system ("Pause" );
return 0;
}
At the very beginning, in global area, I initialized size=10.
But if I want to put this in my parameterized constructor, it shows error. Why?
Mar 5, 2014 at 12:40pm UTC
Instead of initialize in global area, I want to initialize the size in private, and then set its default value if default constructor vector(). But it shows an error. Why?
1 2 3 4 5
vector()
{
size=10;
v = new T[size];
}
Mar 5, 2014 at 12:55pm UTC
because you use that variable in a few others places that are outside the scope of your first constructor?
Topic archived. No new replies allowed.