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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:51:10+00:00 2026-05-25T17:51:10+00:00

I am trying to access a method of an object ( myEOS.calc(…) ) from

  • 0

I am trying to access a method of an object (myEOS.calc(...)) from a method of an object (myPlanet) of another class containing an instance of the first class (static EOS myEOS):

// class in class

class EOS {
  public:
    static float Y;
    float calc(float);
};
float EOS::calc(float x){
  return x;                      // some complicated calculation
}

class Planet {
  public:
    static EOS myEOS;            // want only one instance; cf. below
    static void setX(float* X);  // must be static; cf. below
};
void Planet::setX(float* X) {
  *X = myEOS.calc(*X);           // will give an error
}

int main(){
  Planet myPlanet;
}

This returns the linking-time error

In function `Planet::setX(float*)':
test.cpp:(.text+0x1a): undefined reference to `Planet::myEOS'
collect2: ld returned 1 exit status

Compiling separately, with -c, the classes and the main program (with an #include in the main file) gives no error; this looks like the key to the solution but I do not see the lock!

Does someone know what the problem is? I hope my intent is clear from what I have, even if there is some fundamental misconception. I thought I somewhat understood classes and reread tutorials but saw no discussion of classes within classes (not nested classes). I was not able to find a similar question on this site (from which I can usually get all the answers I need!) either.

By the way, following somebody else’s question, adding explicit constructors (and correctly initialising the EOS in Planet’s initialiser list) did not remove the compiler complaint about “undefined reference to Planet::myEOS'" (this done without thestatic` keyword).

Finally, note that Planet::setX needs to be static because a pointer to this method must have a “class-less” signature as it is passed to a function that cannot handle methods/classes:

 void (*fun_ptr)(float*) = & (Planet::setX);

This also forces the object myEOS to be static (needs to be accessed from a static function), and anyway the initialisation of EOS objects is expensive.

Thanks a lot for anybody’s help!

  • 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-25T17:51:11+00:00Added an answer on May 25, 2026 at 5:51 pm
    class Planet {
      public:
        static EOS myEOS;            // want only one instance; cf. below
    

    This code says “Compiler: Somewhere later will be a global EOS myEOS for this class. And the compiler says “ok”, and does it’s thing, waiting for you to tell it where the global EOS myEOS is.
    Then the linker comes along to clean up the compiler’s mess, and says “I can’t find the myEOS, where’d you place the myEOS?” And displays an error.

    You need to add the following lines to a CPP file somewhere:

    float EOS::Y;
    EOS Planet::myEOS;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to access a static method in a Utils class I created: public
Hey I'm trying to access an object I've made in the main method from
We have the following method where we are trying to access an array object
I am trying to access a page method from centralized module. I tried putting
I tried to return the reference of FMResult object from a method and trying
I'm trying to access a property of an object inside a method of that
I have an NSDictionary object called myData, and I'm trying to access it from
I'm trying to access a method that is marked as internal in the parent
Trying to access a Servlet from a button on HTML page //Html Page FORM
Im trying to access the Magento customer session in another part of my website.

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.