在我的标题中说明,试图在我的代码中使用静态方法编译我的文件。我computeCivIndex()
正在尝试从用户那里获取 5 个输入并进行计算并返回浮点值。
this.sunType
适用于 java 语法,但对于 V++,如果两者同名,我应该用什么将它们链接在一起?
我的代码中有 getter 和 setter 方法,还有 2 个构造函数,它们太长而无法发布。
这是我的错误:
test.cpp:159: error: cannot declare member function ‘static float LocationData::computeCivIndex(std::string, int, int, float, float)’ to have static linkage
test.cpp: In static member function ‘static float LocationData::computeCivIndex(std::string, int, int, float, float)’:
test.cpp:161: error: ‘this’ is unavailable for static member functions
编码:
class LocationData
{
private:
string sunType;
int noOfEarthLikePlanets;
int noOfEarthLikeMoons;
float aveParticulateDensity;
float avePlasmaDensity;
public:
static float computeCivIndex(string,int,int,float,float);
};
static float LocationData::computeCivIndex(string sunType, int noOfEarthLikePlanets,int noOfEarthLikemoons, float aveParticulateDensity, float avePlasmaDensity)
{
this.sunType = sunType;
this.noOfEarthLikePlanets = noOfEarthLikePlanets;
this.noOfEarthLikeMoons = noOfEarthLikeMoons;
this.aveParticulateDensity = aveParticulateDensity;
this.avePlasmaDensity = avePlasmaDensity;
if(sunType == "Type O")
//and more for computation
}