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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:00:36+00:00 2026-05-10T20:00:36+00:00

i am trying to compile this very simple piece of code class myList {

  • 0

i am trying to compile this very simple piece of code

class myList { public:     std::vector<std::string> vec;     class Items     {     public:         void Add(std::string str)         {             myList::vec.push_back(str);         };     }items; };  int main() {     myList newList;     newList.items.Add('A'); } 

what can i do to make this work without creating more objects that needed or overcomplicating stuff…

  • 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. 2026-05-10T20:00:37+00:00Added an answer on May 10, 2026 at 8:00 pm

    Add a couple of constructors and a pointer to the parent class.

    #include <string> #include <vector> class myList { public:     std::vector<std::string> vec;     myList(): items(this) {} // Added     class Items     {     public:         Items(myList *ml): self(ml) {}  // Added         void Add(std::string str)         {                 self->vec.push_back(str); // Changed         };         myList *self; //Added     }items; };  int main() {     myList newList;     newList.items.Add('A'); } 

    You need the myList() constructor, so it registers instances of itself with the instance of the inner class member variable. Then you need the Items constructor to store the pointer to the outer myList class instance. Finally in the Add method, you need to reference vec in the stored myList instance.

    As Catskul points out, the Item constructor mustn’t actually do anything with the myList pointer it receives. I’d also like to say that though this answer is closer to the original intent, steveth45’s answer is closer to what you would want to do in a real program.

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

Sidebar

Related Questions

When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I am trying to compile the following very very simple piece of source code:
I'm trying to write a very simple piece of code and can't figure out
Look at this very simple piece of code: struct A { char* s; };
I trying to compile this code: Int64 itag = BitConverter.ToInt64(temp, 0); itag &= 0xFFFFFFFFFFFFFC00;
I'm trying to compile this simple program to start learning how to use timers:
When I'm trying to compile this code with gcc version 4.6.3 on linux with
I am trying to use some sample code and my compiler won't compile this
I am just trying to compile and run a very simple test program, but
I am trying to compile a very simple C program. Installed MinGW using auto

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.