2017-04-22 40 views
1

我試圖從我定義的另一個類調用類函數computeCivIndex(),但它說有一個未定義的引用LocationData :: computeCivIndex(string,int,int,float,float )和一個未定義的函數computeCivIndex(string int,int,float,float)in我的LocationData.cpp 我用g ++ -Wall -W LocationData.h LocationData.cpp PointTwoD.h PointTwoD.cpp MissionPlan.cpp -o myProg .o來編譯。鏈接兩個文件之間的錯誤C++

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; 
+0

該方法是靜態的,不能用對象調用它。 –

回答

0

那是因爲你還沒有實現的功能!

,需要相應地限定功能的實現來告訴你實際上是實現了類函數,編譯器:

SomeClass.h:

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

SomeClass.cpp:

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

如果你只是做

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

您定義了兩個附加的函數在命名空間範圍內,而不是您想要的類範圍。通過將它們聲明爲靜態,您另外限制了它們在其中定義的.cpp文件中的可訪問性,所以在編譯其他.cpp文件時不會找到它。

請注意,類中的靜態與全局作用域(以及函數體內的另一個不同含義)具有完全不同的含義 - 稍後再說。首先,另一個錯誤,但:

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

通過運算符。 (或操作符 - 上指針>),你只能調用非靜態函數(這是從Java不同),靜態函數需要用類範圍被稱爲:

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

static在不同範圍含義:

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();是C方式限制acessibility - 被放置函數爲匿名命名空間中的C++變體:

namespace 
{ 
    void anotherF() { } 
} 

最後:靜態在函數內部:這樣,你聲明瞭一些只能從函數內部訪問的全局變量。它被初始化僅在第一次調用該函數,和之前的值將適用於所有後續調用:

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

輸出將是:1 2 3。 更多細節你找到here