Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:08:02+00:00 2026-06-09T12:08:02+00:00

I am trying to construct an object, and then set one of the variables

  • 0

I am trying to construct an object, and then set one of the variables to a value.

Class header:

class Book {
public:
Book();
Book(string newSelection);
string getSelection();
string setSelection(string newSelection);

private:
string selection;
}

Class cpp

Book::Book() {}

Book::Book(string newSelection) {
selection = newSelection;
}


string Book::getSelection(){
return selection;
}

string Book::setSelection(string newSelection){
selection = newSelection;
}

Driver

Book* book1 = new Book();
book1->setSelection("The Book Title");
cout << "Book selected:    " << book1->getSelection() << endl;

I’m getting a segmentation fault, and am not sure why. Can somebody point out where my problem may be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:08:04+00:00Added an answer on June 9, 2026 at 12:08 pm

    Your setSelection() function is declared as returning a string but no string is actually being returned. You should get a compile warning at least for this.

    From running this through in a debugger, I believe that what’s happening is that following the call to setSelection() the destructor is called on the returned string. As this string does not really exist, this causes an abort() within the runtime.

    As a general rule, ‘setters’ tend not to return anything so would be written as follows:

    void setSelection(const string& newSelection);
    

    …

    void Book::setSelection(const string& newSelection)
    {
        selection = newSelection;
    }
    

    also note that the string is passed in by const reference rather than by value which is more efficient. This won’t be the cause of your problem though.

    Another suggestion would be to ensure that your ‘getter’ is declared const as it doesn’t change anything in the object:

    string getSelection() const;
    

    …

    string Book::getSelection() const
    {
        return selection;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Object initializers to set the properties of a class
I'm trying to make a copy constructor for an object and one of the
I'm trying to discover the most-derived class of an object, within a constructor of
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
So I have a class with a single string parameter in its constructor: public
I am trying to construct a Java File object based on a user provided
I'm trying to use a JNI function to create a Java class and set
I'm trying to create a new object of type T via its constructor when
I'm trying to create a Database object for myself in .net. The constructor takes
Trying to construct a Request with Network.HTTP.Conduit package. The instructions are: The constructor for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.