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

  • Home
  • SEARCH
  • 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 8816215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:34:05+00:00 2026-06-14T04:34:05+00:00

Possible Duplicate: Can inner classes access private variables? Inner class accessing outer class I

  • 0

Possible Duplicate:
Can inner classes access private variables?
Inner class accessing outer class

I have some simple classes nested so they can interact with a variable without extra inputs, yet my compiler gives me an error. How would I allow for them to interact without using &time as function input or having the variable &time inside of the Vect class?

I tried using the same logic where you can have data accessed that is just in the code, at the same place as function prototypes, but is instead wrapped in a class. This works fine for anything I’ve used except other classes. Can anyone explain why?

I have marked the places that use the problematic time variable with comment lines like the one before the define.

/*********/
#define MAX_POLY 3

class Container
{
public:
    Container(void);
    ~Container(void);

    float time;/*********/
    class Vect
    {
        float P[MAX_POLY],iT;
    public:
        Vect(void){iT = 0.0f;P = {0,0,0};}
        ~Vect(void);

        float GetPoly(int n){return P[n];}
        float Render(void)
        {
            float t = time - iT;/*********/
            float temp[2] = {0,0};
            for(int n=0;n<MAX_POLY;n++)
            {
                temp[0] = P[n];
                for(int m=0;m<n;m++)
                    temp[0] *= t;
                temp[1] += temp[0];
            }
            return temp[1];
        }
        void SetPoly(int n,float f)
        {
            float t = time-iT;/*********/
            P[0] = P[2]*t*t + P[1]*t + P[0];
            P[1] = 2*P[2]*t + P[1];
            //P[2] = P[2];
            P[n] = f;
            iT = time;/*********/
        }
    }X;
};

int main()
{
    Container Shell;
    Shell.X.SetPoly(0,5);
    Shell.X.SetPoly(1,10);
    Shell.X.SetPoly(2,-1);
    for(int n=0;n<10;n++)
    {
        Shell.time = (float)n;
        cout << n << " " << Shell.X.Render() << endl;
    }
    system("pause");
    return 0;
}
  • 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-14T04:34:07+00:00Added an answer on June 14, 2026 at 4:34 am

    The reason you get an error (I managed to figure it out even though you didn’t post the actual error, please do that in the future), is that you actually don’t have an instance of the Container class inside the functions of the Vect class. You should probably think about the design here, but to solve it quickly (and “dirty”) you could add a function which sets a Container instance in the sub-class:

    class Container
    {
        ...
    
        class Vect
        {
            Container *container;
    
        public:
            void SetContainer(Container &container)
                { this->container = &container; }
    
            float Render(void)
                {
                    float T = container->time - iT;
                    ...
                }
    
            ...
        } X;
    };
    
    int main()
    {
        Container Shell;
        Shell.X.SetContainer(Shell);
        Shell.X.SetPoly(0,5);
        ...
    }
    

    Edit: A better way is to set a reference to the parent object (thanks to juanchopanza for the idea) using the constructors:

    class Container
    {
        ...
    
        Container()
            : X(*this)
            { ... }
    
        class Vect
        {
            Container& container;
    
        public:
            Vect(Container& cont)
                : container(cont)
                { }
    
            float Render(void)
                {
                    float T = container.time - iT;
                    ...
                }
    
            ...
        } X;
    };
    

    I still think it’s kind of a dirty solution (but not as dirty as my first), and that you should think about changing the design instead.

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

Sidebar

Related Questions

Possible Duplicate: Use of class definitions inside a method in Java Can we have
Possible Duplicate: Regex - nested patterns - within outer pattern but exclude inner pattern
Possible Duplicate: Java inner class and static nested class An instance of a static
Possible Duplicate: Why cant we have static method in an inner class? Hi all,
Possible Duplicate: Select inner text (jQuery) I have a span: <span class=test> target_string </span>
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Anonymous vs named inner classes? - best practices? When working with Java
Possible Duplicate: Why does Java prohibit static fields in inner classes? I was going
Possible Duplicate: Usage of inner class I was looking for main reason for which,

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.