我正在尝试构造一个对象,然后将其中一个变量设置为一个值。
类头:
class Book {
public:
Book();
Book(string newSelection);
string getSelection();
string setSelection(string newSelection);
private:
string selection;
}
类 cpp
Book::Book() {}
Book::Book(string newSelection) {
selection = newSelection;
}
string Book::getSelection(){
return selection;
}
string Book::setSelection(string newSelection){
selection = newSelection;
}
司机
Book* book1 = new Book();
book1->setSelection("The Book Title");
cout << "Book selected: " << book1->getSelection() << endl;
我遇到了分段错误,不知道为什么。有人可以指出我的问题可能出在哪里吗?