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 8505963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:27:54+00:00 2026-06-11T02:27:54+00:00

I have a class which made of a ints and strings, but I also

  • 0

I have a class which made of a ints and strings, but I also have a vector inside that class. I have to read the records from a file and then after parsing each line, put the info in my vector of class. I have to get the basic package information like ID and name and then add the services which are offered with that package, so I can have 10 records that are from one package but they are different in type of services. For now I am trying to work out putting the data in each package and access the data from each element, but when I am trying to get the data from the vector inside the class, my compiled file crashes. It also prints out the 1233 and foo, but not the test. Any ideas why is that?

int main()
{
    vector<package> packs;
    package pack;
    pack.ID = 1233;
    pack.name = "foo";
    packs.push_back(pack);

    pack.putData("test",12);




     cout << packs[0].name << endl;
     cout << packs[0].ID << endl;
     cout << packs[0].bservice[0].serviceID << endl;    //[b]Crashes in this line[/b]

    return 0;

}

Defined class is:

class package
{
    public:

    class aservice
    {
       public:
       int serviceID;
       string othername;
    };
    int ID;
    string name;
    vector<aservice> bservice;
    void putData(string name1, int serviceID1)
    { 
        aservice obj;
        obj.serviceID = serviceID1;
        obj.othername = name1;
        bservice.push_back(obj);
    }

};
  • 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-11T02:27:56+00:00Added an answer on June 11, 2026 at 2:27 am

    Here you make a copy of pack when you push_back into the vector:

    packs.push_back(pack);
    

    And here you access pack, not the copy stored in your vector

    pack.putData("test",12);
    

    So the bservice vector you are trying to access is actually empty, which is why your code crashes when you try to access it here:

    cout << patients[0].bservice[0].serviceID << endl; // patients[0].bservice is empty!!!
    

    You can avoid this by pushing back after the call to putData:

    vector<package> packs;
    package pack;
    pack.ID = 1233;
    pack.name = "foo";
    pack.putData("test",12);
    packs.push_back(pack);
    

    You can also avoid it by not trying to access a vector without first checking whether it is empty.

    Ideally you should strive to design classes than can be constructed into a useful state, as opposed to default constructing them and adding data step by step via setters. This is particularly important if the data are inter-related and the class must maintain invariants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made my own exception class which derives from runtime_error and is getting
I have a class that implements an interface, which is made available for plugins.
Just a simple question: I have read that a class should be made static
I have made a class which a form can inherit from and it handles
I have made an class which conforms to the NSCoding protocol and does all
I have made a front end for a program which test java (foo.class) binary
I have generic async task class which fetches response from server . And i
I have method in Class which is implementation of Interface. When I made it
I have a class library which is called through COM from a Third Party
I have a class that is created from an enum that can be tested

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.