1

I have tried to call the class function computeCivIndex() from another class I have defined but it says that there was an undefined reference to LocationData::computeCivIndex(string, int, int, float, float) and a defined an unused funciton computeCivIndex(string int, int, float, float) in my LocationData.cpp And I used g++ -Wall -W LocationData.h LocationData.cpp PointTwoD.h PointTwoD.cpp MissionPlan.cpp -o myProg.o to compile.

LocationData.h

static float computeCivIndex( string sunType_, int noOfEarthLikePlanets_, int noOfEarthLikeMoons_, float aveParticulateDensity_, float avePlasmaDensity_ ); /*calculate the possibility of life in the star system*/
};

LocationData.cpp

static float computeCivIndex( string sunType_, int noOfEarthLikePlanets_, int noOfEarthLikeMoons_, float aveParticulateDensity_, float avePlasmaDensity_ )
{
    int sunTypePercent = 0;
    float civIndex;
    // convert string suntype to suntype percent
    if (sunType_ == "Type O")
    {
        sunTypePercent = 30;
    }
    if (sunType_ == "Type B")
    {
        sunTypePercent = 45;
    }
    if (sunType_ == "Type A")
    {
        sunTypePercent = 60;
    }
    if (sunType_ == "Type F")
    {
        sunTypePercent = 75;
    }
    if (sunType_ == "Type G")
    {
        sunTypePercent = 90;
    }
    if (sunType_ == "Type K")
    {
        sunTypePercent = 80;
    }
    if (sunType_ == "Type M")
    {
        sunTypePercent = 70;
    }
    //compute the CivIndex
    civIndex = ( (sunTypePercent/100) - (aveParticulateDensity_ + avePlasmaDensity_)/200 ) * 
        (noOfEarthLikePlanets_ + noOfEarthLikeMoons_);
    return civIndex;
}

MissionPlan.cpp

float computeCivIndex[arraySize];

//compute civ index for all stored records 
for (int i = 0; i < (entryNo+1); i++)
{
    string suntype          = locData[i].getSunType();
    int earthlikeplanets    = locData[i].getNoOfEarthLikePlanets();
    int earthlikemoons      = locData[i].getNoOfEarthLikeMoons();
    float partdensity       = locData[i].getAveParticulateDensity();
    float plasdensity       = locData[i].getAvePlasmaDensity();
    locData[i].computeCivIndex(suntype,earthlikeplanets, earthlikemoons , partdensity, plasdensity);
    point2d[i].setCivIndex(computeCivIndex[i]);

}

cout << "Computation Completed! ( " << entryNo <<" records were updated )" << endl;
Monomoni
  • 415
  • 2
  • 4
  • 19

1 Answers1

0

That is because you have not implemented the function!

You need to qualify the function appropriately in the implementation to tell the compiler that you are actually implementing the class functions:

SomeClass.h:

class SomeClass
{
    void someFunction();
    static void someStaticFunction();
};

SomeClass.cpp:

void SomeClass::someFunction() { }
void SomeClass::someStaticFunction() { } // static must not be repeated!

If you do simply

void someFunction() { }
static void someStaticFunction() { }

you define two additional functions at namespace scope, not at class scope as you intended. By declaring them static, you additionally limit accessibility to from within the .cpp file they are defined in, so won't be found when compiling other .cpp files.

Be aware that static within a class has a completely different meaning than at global scope (and another different meaning within a function body) - Coming to that later. First, another error, though:

locData[i].computeCivIndex(suntype,earthlikeplanets, earthlikemoons , partdensity, plasdensity);

Via operator. (or operator-> on pointers), you can only call non-static functions (this is different from Java), static functions need to be called with class scope:

LocationData::computeCivIndex(suntype,earthlikeplanets, earthlikemoons, partdensity, plasdensity);

Meaning of static at different scopes:

class SomeClass
{
    static void f(); // static class function as you know
};

static void anotherF(); // free/global function - static limits accessibility!

void yetAnotherF()
{
    static int n = 0; // see below
}

static void anotherF(); is the C way to limit acessibility - the C++ variant of is placing the function into an anonymous namespace:

namespace
{
    void anotherF() { }
}

Finally: static within functions: This way, you declare some kind of global variable that is only accessible from within the function. It is initialised only the first time you call the function, and the previous value of will be available on all subsequent calls:

void f()
{
    static int x = 0;
    printf("%d ", ++x);
}
int main()
{
    f();
    f();
    f();
    return 0;
}

Output will be: 1 2 3. More details you find here.

Community
  • 1
  • 1
Aconcagua
  • 24,880
  • 4
  • 34
  • 59