I just started using Smart Pointers in C++, and ran into some trouble, but can't figure out where I went wrong. I have a class Seat
, and SeatSmart
having pointer to Seat
class. These are my classes:
class Seat{
public:
int val;
int idx;
Seat(int v, int i):val(v),idx(i){}
};
class SeatSmart{
public:
Seat *seat;
SeatSmart():seat(NULL){}
SeatSmart(Seat *st):seat(st){}
~SeatSmart(){
delete(seat);
}
}
After this I ran the following code in main
:
priority_queue<SeatSmart> pq;
vector<SeatSmart> q;
SeatSmart sm;
for(int i = 0 ; i < 10 ; i++){
sm.seat = new Seat(i, i);
pq.push(sm);
q.push_back(sm);
}
I also overloaded the <
operator:
bool operator<(const SeatSmart &a, const SeatSmart &b){
return a.seat->val < b.seat->val;
}
The problem arises after adding the second element in the priority queue, when the operator <
is called. The system shows a segmentation fault at that point according to GDB.
The copy constructor should be called when adding sm
to the vector and the priority queue. Can you please tell me where I went wrong.