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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:46:15+00:00 2026-06-06T12:46:15+00:00

I am making an hierarchy of sorts and am having trouble adding an element

  • 0

I am making an hierarchy of sorts and am having trouble adding an element to a vector. I simplified the code and still cannot add an element to the vector as expected. The hierarchy looks like:

Pdb > Chain > strings

Pdb and Chain are class names and strings is the name of a vector belonging to Chain. I cannot push_back to add a string to strings. You may get a better idea looking at my code:

Chain Class:

class Chain {
  string chain_id;
  vector<string> strings;

public:
  Chain(string id_) { chain_id = id_; }
  vector<string> GetStrings() { return strings; }
  void AddString(string s) { 
    cout << "Size of strings BEFORE push_back in AddString: " << strings.size() << endl;
    strings.push_back(s); 
    cout << "Size of strings AFTER push_back in AddString: " << strings.size() << endl;
  }
  string GetChainId() { return chain_id; }
};

Pdb class:

class Pdb {
  string pdb_id;
  vector<Chain> chains;

public:
  Pdb(string id_) { pdb_id = id_; }
  vector<Chain> GetChains() { return chains; }
  void AddChain(Chain c) { chains.push_back(c); }
  string GetPdbId() { return pdb_id; }
};

main:

int main () {

  vector<Pdb> pdbs;
  pdbs.push_back(Pdb("1ubq"));
  cout << "\n\t1. " << pdbs[0].GetPdbId() << endl;
  pdbs[0].AddChain(Chain("A"));
  cout << "\n\t2. " << pdbs[0].GetChains()[0].GetChainId() << endl;
  pdbs[0].GetChains()[0].AddString("Whateva");
  cout << "\n\t3. Size of strings after AddString in main: " << pdbs[0].GetChains()[0].GetStrings().size() << endl;
  return 0;
}

This outputs:

1. 1ubq

2. A
Size of strings BEFORE push_back in AddString: 0
Size of strings AFTER push_back in AddString: 1

3. Size of strings after AddString in main: 0

As you can see the AddString function is adding an element to stings within the AddString function itself but when I GetStrings back in main, after executing AddString, strings is empty. I do not understand why this is happening. Any help would be much appreciated.

  • 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-06T12:46:17+00:00Added an answer on June 6, 2026 at 12:46 pm

    The problem is that you return a copy of the member, not the member itself:

    vector<Chain> GetChains()
    

    should be

    vector<Chain>& GetChains()
    

    for this to work.

    I must note that you’re heavily breaking the single responsibility principle. You’re operating on members directly, which can’t be a good idea. Consider replacing:

    pdbs[0].GetChains()[0].AddString("Whateva");
    

    with something like

    pdbs[0].AddStringToChain(0,"Whateva");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've been having trouble with this issue for a while, and just cannot find
I have the following UI element hierarchy: UserControl>ScrollViewer>Canvas. I am drawing lots of stuff
I'm having problems designing a service layer when using a Table per Hierarchy setup
I am making a small app with the following view hierarchy with UINavigationController: Login
I am programmatically building a UINavigationContoller for iOS and am having problems making it
I was wondering if making code more explicit helps Eclipse to analyse it faster.
Im making a herbal database program and I am having troubles setting up HierarchicalDataTemplates
I am making a blog application and want to automatically add the current user
Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view
Making lots of progress in a rails 3.1 to 3.2.6 upgrade for a Heroku

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.