Labbedudl
Labbedudl

Reputation: 17

Accessing a method of a member object in C++

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!

Upvotes: 0

Views: 117

Answers (2)

Mooing Duck
Mooing Duck

Reputation: 66981

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;

Upvotes: 2

Kashyap
Kashyap

Reputation: 17546

Define the static variable.

// 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
}

// -------------------------
EOS Planet::myEOS;
float Planet::Y /* = <some value> if it makes sense*/;
// -------------------------

int main(){
  Planet myPlanet;
}

See 9.4.2 Static Data Members in the standard (98).

Upvotes: 2

Related Questions