Can somebody tell, why I have segmentation fault in that code? I have no idea why is that. I use Code:Blocks but online compilers have the same problems. I don't know where the problem is.
#include <iostream>
#include <queue>
#include <memory>
using namespace std;
class Task {
private:
queue <string> q;
public:
string input;
void read (int hm)
{
for (int i=1;i<=hm;i++)
{
cin>>input;
q.push(input);
}
}
void count()
{
cout<<q.back();
}
};
int main()
{
unique_ptr <Task> ptr;
int how_many;
cin>>how_many;
ptr->read(how_many);
ptr->count();
return 0;
}
The pointer ptr
is being used without being initialized. Use:
std::unique_ptr<Task> ptr = std::make_unique<Task>();
That being said you should also include the <string>
header explicitly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.