2017-04-22 7 views
1

私が定義した別のクラスからクラス関数computeCivIndex()を呼び出そうとしましたが、LocationData :: computeCivIndex(string、int、int、float、float )と私のLocationData.cppに定義されていない関数computeCivIndex(文字列int、int、float、float) そして私はg ++ -Wall -W LocationData.h LocationData.cppを使いました。PointTwoD.h PointTwoD.cpp MissionPlan.cpp -o myProg .oをコンパイルする。2つのファイル間のリンクエラー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() { } 

は、名前空間のスコープで2つを定義し、意図したとおりにクラススコープでは定義しません。それらを静的と宣言することによって、定義された.cppファイル内からアクセシビリティを制限することができます。したがって、他の.cppファイルをコンパイルするときにはアクセスできません。

クラス内の静的な意味は、グローバルスコープとは全く異なる意味を持ちます(関数本体内の別の意味)。ただし、別のエラーもあります。

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

Viaオペレータ。 (またはoperator->ポインタに)、あなたは唯一の非スタティック関数(これは、Javaと異なっている)を呼び出すことができ、静的関数は、クラススコープで呼び出される必要があります。異なるスコープでstatic

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

は意味:

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();はacessibilityを制限するためのCの方法です - のC++の変異体は、匿名の名前空間に関数を配置している。最後に

namespace 
{ 
    void anotherF() { } 
} 

:静的関数内でのみアクセス可能な何らかの種類のグローバル変数を宣言します。それはあなたが関数を呼び出すだけで初めて初期化され、そして以前の値は、すべての後続の呼び出しに利用できるようになります:

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

出力は次のようになります。1 2 3。 詳細はhereです。

関連する問題