0

我有 3 个类 LocationData、PointTwoD 和 MissionPlan。类 MissionPlan 是我的驱动程序类,它将运行我的所有程序。在我的 LocationData 中,有一个静态方法接收来自用户的 5 个输入,它将计算为浮点数并返回值。如前所述,MissionPlan 是我的驱动程序类,所有输入都将链接到我的 MissionPLan 类中,我尝试在 MissionPlan 中调用静态方法来计算值 LocationData::computeCivIndex(s,i,j,k, l); 但它返回给我一个很长的错误。

/tmp/ccK8Rwha.o: 在函数MissionPlan::MissionPlan()': test.cpp:(.text+0x9f1): undefined reference toLocationData::computeCivIndex(std::basic_string, std::allocator >, int, int, float, float)' /tmp/ccK8Rwha.o: 在函数‘MissionPlan::MissionPlan() ':

test.cpp:(.text+0xfb7): undefined reference to `LocationData::computeCivIndex(std::basic_string, std::allocator >, int, int, float, float)' collect2: ld 返回 1 个退出状态

class LocationData
{   
    private:
        //codes for initilization
    public:
    LocationData();
    LocationData(string,int,int,float,float);
    //getter and setter methods..
    static float computeCivIndex(string,int,int,float,float);
};

class MissionPlan
{
    public:
    MissionPlan();
};

static float computeCivIndex(string sunType, int noOfEarthLikePlanets,int     noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
{
    LocationData data; //not sure if i need this
        //methods to caculate...
    float ci = 0.0;
        return ci;
}

MissionPlan::MissionPlan()
{
        int choice; // to capture what user inputs into menu
        int count=0; 
            //codes to get user inputs
        cin>>choice;
        for(;;)
        {
        if(choice == 1)
        {       
            int i,j,x,y;
        float k,l;
         string s;
       //codes to get user inputs
        LocationData::computeCivIndex(s,i,j,k,l); //the part where error occurs
         count++;
     }
    else if(choice == 2)
    {
        cout<<"Computation completed!"<<endl;
        break;
    }
    else if(choice==3);
    else if(choice==4);
    else
        cout<<"Please enter number 1 to 4 only!"<<endl;
    }//end of do loop
}//end of MissionPlan()
int main()
{
    MissionPlan plan;
    MissionPlan();
    return 0;
}
4

3 回答 3

0

Two errors from what you've posted:

static float computeCivIndex(string sunType, int noOfEarthLikePlanets,int     noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
{
    LocationData data; //not sure if i need this
        //methods to caculate...
    float ci = 0.0;
        return ci;
}

This function should be:

float LocationData::computeCivIndex(string sunType, int noOfEarthLikePlanets,int     noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
{
    LocationData data; //not sure if i need this
        //methods to caculate...
    float ci = 0.0;
        return ci;
}

Secondly, I don't know how much this is correct:

in your main function: you defined something that doesn't seem right

MissionPlan();

You're calling a constructor without an object? Although its valid syntax, and the constructor will be called.. why do it twice?

于 2012-10-21T16:18:10.310 回答
0

你还没有实现静态方法——你已经创建了另一个。

试试这个:

float LocationData::computeCivIndex...

在您实施它的地方。

于 2012-10-21T15:45:00.957 回答
0

您声明了 LocationData 类的静态方法,但从未定义它。

改变

static float computeCivIndex(string sunType, int noOfEarthLikePlanets,int     noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)

float LocationData::computeCivIndex(string sunType, int noOfEarthLikePlanets,int     noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
于 2012-10-21T15:47:15.730 回答