내 코드에 정적 메서드로 파일을 컴파일하려고 시도했습니다. 내 computeCivIndex()
은 사용자로부터 5 개의 입력을 가져 와서 계산을 수행하고 float 값을 되 돌리려고합니다.정적 메서드로 컴파일하는 동안 오류가 발생했습니다.
this.sunType
은 java 구문이지만 V ++의 경우 둘 모두가 동일한 이름 인 경우 어떻게 연결해야합니까?
내 코드에는 getter 및 setter 메서드가 있고 게시하기에는 너무 긴 두 생성자가 있습니다.
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
}
내 숙제는 정적 메소드 여야하므로 링크 할 수있는 방법이 있습니까? –
@HengAikHwee : 이상한 요구 사항처럼 들립니다. 인스턴스에 대한 참조를 가져 오기 위해 서명을 변경할 수 있습니까? 대신 새 인스턴스를 반환 할 수 있습니까? –
흠, LocationData 데이터를 의미합니까? data.sunType = sunType; ? –