我試圖從我定義的另一個類調用類函數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;
該方法是靜態的,不能用對象調用它。 –