C++ Vector class object pointer how to print

#include <iostream>
#include <vector>

using namespace std;


class MyClass
{
public:
int price;
MyClass()
{
cin>>price;
}

};

class Evet
{
public:
std::vector<MyClass*> v;

double overall()
{
int n;
int sum = 0;
cout<<"Enter how many"<<endl;
cin>>n;
v.push_back(new MyClass [n]);
for(int i = 0; i < n; i++)
{
cout<<v[i]->price<<endl;
}

}
};

int main()
{

Evet e;

e.overall();


return 0;
}

I get segmentation fault(core dumped) when trying to cout V[i>0]->price
What am I doing wrong
Hi,
1
2
3
4
for(int i = 0; i < n; i++)
 {
    cout<< v[i]->price << endl;
 }


Should be :
1
2
3
4
for(int i = 0; i < n; i++)
 {
     cout<< v.back()[i].price << endl;
 }
Last edited on
Does that help you? :)
Topic archived. No new replies allowed.