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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:59:41+00:00 2026-05-22T00:59:41+00:00

class A{ public: template<typename Obj> class MyVec{ //some methods… }; MyVec<A> a; //-> doesnt

  • 0
class A{
public:
      template<typename Obj>
      class MyVec{
           //some methods...
      };
      MyVec<A> a; //-> doesnt work
      //vector<A> a; //using stl vector-> this works
};

class B{
public:
      void someMethod();
private:
      A::MyVec<A> b;
};

In the method, when I do sth like:

void someMethod(){
     //...
     b[0].a.pushback(element);
     //...
}

In class A if i use std::vector everything works properly. But when i use nested class it doesn’t work.

  • 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-05-22T00:59:42+00:00Added an answer on May 22, 2026 at 12:59 am

    I’ve taken your code and modified it as minimally as possible to get something that compiles. This seems to work fine. And so the error is not in the code you have shown us.

    #include <iostream>
    #include <string>
    
    class A
    {
      public:
        template<typename Obj>
          class MyVec{
            public:
            //My pushback just stores the value away for later.
            void pushback(Obj & o )
            {
              if( obj )
              {
                *obj = o;
              }
              else
              {
                obj = new Obj(o);
              }
              std::cout<<this<<" : Pushing object "<<&o<<std::endl;
            }
    
            //some methods...
            //My operator[] just returns the stored object.
            Obj& operator[](int i) { return *obj; }
            Obj * obj;
            MyVec() : obj( NULL ) {}
          };
        MyVec<A> a;
    };
    
    
    class B
    {
    public:
    B()
    {
       A an_a;
       b.pushback(an_a); //Better store one away since we access it in someMethod.
    }
    void someMethod()
    {
         //...
         A element;
         b[0].a.pushback(element);
         //...
    }
    private:
          A::MyVec<A> b;
    };
    
    int main()
    {
       //Test that it all works
       B outer;
       outer.someMethod();
    }
    

    When I run this I get:

    0xbffffa5c : Pushing object 0xbffffa0c
    0x3ec3c0 : Pushing object 0xbffffa2c
    

    Which is what I’d expect. (one push in the constructor of B and one push to the internal object from someThing)

    You can view the result here:
    http://ideone.com/BX8ZQ

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

Sidebar

Related Questions

Here the code #include <iostream> #include <conio.h> using namespace std; template <typename T> class
My question is related to this question. #include<iostream> template< typename T > class T1
class A { public: template<typename T> void func(size_t n, T values[]) { ... }
Is following design possible?: template <typename T> class Test{ public: template <typename Z> void
Let's say we have a class, MyParent: class MyParent { public: template<namespace T> MyParent()
I have got a template class as follows: class MyClass<T> { T field; public
This is my action class public Iterator<CgConsultant> getSearchresult() { List<CgConsultant> list =userSearch.getConsultantMatches(searchstring); System.out.println(The size
I have a template based class [Allotter.h & Allotter.cpp]: template <typename allotType> class Allotter
I have something like this: class Base { public: static int Lolz() { return
My code does not compile. Below is my code template <typename T> class TemplateClass

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.