C++ 尝试将静态方法从一个类转换到另一个类
我有3门课LocationData,PointTwoD和MissionPlan。 类MissionPlan是我的驱动程序类,它将运行我的所有程序。 在我的LocationData中,有一个静态方法,它接收用户的5个输入,然后计算成一个浮点值并返回值。 正如前面提到的MissionPlan是我的驱动程序类,所有的输入都将链接到我的MissionPlan类中,我尝试在MissionPlan中调用静态方法来计算值LocationData::computeCivIndex(s,i,j,k,l);但这给我带来了一个很长的错误 /tmp/ccK8Rwha.o:在函数C++ 尝试将静态方法从一个类转换到另一个类,c++,class,static,C++,Class,Static,我有3门课LocationData,PointTwoD和MissionPlan。 类MissionPlan是我的驱动程序类,它将运行我的所有程序。 在我的LocationData中,有一个静态方法,它接收用户的5个输入,然后计算成一个浮点值并返回值。 正如前面提到的MissionPlan是我的驱动程序类,所有的输入都将链接到我的MissionPlan类中,我尝试在MissionPlan中调用静态方法来计算值LocationData::computeCivIndex(s,i,j,k,l);但这给我
MissionPlan::MissionPlan()'中:
test.cpp:(.text+0x9f1):对
LocationData::computeCivIndex(std::basic_string,std::allocator>,int,int,float,float)的未定义引用
/tmp/ccK8Rwha.o:在函数'MissionPlan::MissionPlan()'中:
test.cpp:(.text+0xfb7):未定义对“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;
}
类位置数据
{
私人:
//初始化代码
公众:
位置数据();
LocationData(字符串、整数、整数、浮点、浮点);
//getter和setter方法。。
静态浮点计算CIVINDEX(字符串,int,int,float,float);
};
班级任务计划
{
公众:
任务计划();
};
静态浮点计算CIVINDEX(字符串sunType、int noOfEarthLikePlanets、int NoofearthlikeMons、浮点A特定密度、浮点A等离子体密度)
{
LocationData;//不确定我是否需要这个
//计算方法。。。
浮动ci=0.0;
返回ci;
}
任务计划::任务计划()
{
int choice;//捕获用户在菜单中输入的内容
整数计数=0;
//获取用户输入的代码
cin>>选择;
对于(;;)
{
如果(选项==1)
{
int i,j,x,y;
浮动k,l;
字符串s;
//获取用户输入的代码
LocationData::computeCivIndex(s,i,j,k,l);//发生错误的部分
计数++;
}
else if(选项==2)
{
cout您还没有实现静态方法-您已经创建了另一个
请尝试以下方法:
float LocationData::computeCivIndex...
在实现它的地方。您声明了类LocationData的静态方法,但从未定义它
改变
static float computeCivIndex(string sunType, int noOfEarthLikePlanets,int noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
到
您发布的内容中有两个错误:
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;
}
此功能应为:
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;
}
其次,我不知道这有多正确:
在main
函数中:您定义了一些似乎不正确的内容
MissionPlan();
您正在调用一个没有对象的构造函数?虽然它的语法有效,但构造函数将被调用。为什么要调用两次?我得到一个错误测试。cpp:在构造函数的MissionPlan::MissionPlan()中“:test.cpp:253:错误:限定名'LocationData::computeCivIndex'的使用无效。它对我来说编译得很好。你确定你更改了正确的内容吗?类定义应该保持不变,你在computeCivIndex
下面实现任务计划
的位置应该如我所示。我尝试添加了它,我得到了它。”s错误测试。cpp:169:错误:无法声明成员函数“static float LocationData::computeCivIndex(std::string,int,int,float,float)”具有静态链接oops..删除静态。只需float LocationData::computeCivIndex(字符串sunType、int noOfEarthLikePlanets、int noofearthlikemons、float ave特殊密度、float ave等离子体密度)@HengAikHwee编辑了我的答案以消除静电干扰
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;
}