B
Babis Haldas
Can you please tell me whats wrong with the follow code
Its crasing in the line
cout << v[0]->x1 << endl;
when accesing the first element of the vector
If i define the vector as
vector<foo*>v ;
everything is ok
why it doesnt like v(10) ?
thanks
#include <iostream>
#include <vector>
using namespace std;
struct foo {
int x1;
int x2;
};
int main()
{
vector<foo*> v(10);
foo * p;
p = new foo;
p->x1 = 111;
p->x2 = 222;
v.push_back(p);
cout << v[0]->x1 << endl;
}
Its crasing in the line
cout << v[0]->x1 << endl;
when accesing the first element of the vector
If i define the vector as
vector<foo*>v ;
everything is ok
why it doesnt like v(10) ?
thanks
#include <iostream>
#include <vector>
using namespace std;
struct foo {
int x1;
int x2;
};
int main()
{
vector<foo*> v(10);
foo * p;
p = new foo;
p->x1 = 111;
p->x2 = 222;
v.push_back(p);
cout << v[0]->x1 << endl;
}