求助一个c++类的问题(很简单的)
Planet.h内容如下#include<iostream>
#include<string>
using namespace std;
class Planet
{
public:
float otherPlanet();
void setPlanet();
string returnplanet();
Planet(float intweight,string other);
Planet();
private:
float weight;
string planet;
};
cilent.cpp内容如下
#include<iostream>
#include<string>
#include"Planet.h"
using namespace std;
float Planet::otherPlanet()
{
switch(planet[0])
{
case 'M':
switch(planet[1])
{
case 'e':return weight*0.4155;
break;
case 'o':return weight*0.166;
break;
case 'a':return weight*0.3507;
}
break;
case 'V':return weight*0.8975;
break;
case 'E':return weight*1.0;
break;
case 'J':return weight*2.5374;
break;
case 'S':return weight*1.0677;
break;
case 'U':return weight*0.8947;
break;
case 'N':return weight*1.1794;
break;
case 'P':return weight*0.0899;
default:
return weight;
}
}
string Planet::returnplanet()
{
return planet;
}
void Planet::setPlanet()
{
cout<<"enter weight and planet"<<endl;
cin >>weight>>planet;
}
Planet::Planet()
{
weight=0.0;
planet="earth";
}
Planet::Planet(float intweight,string other)
{
weight=intweight;
planet=other;
}
test.cpp内容如下
#include<iostream>
#include<string>
#include"Planet.h"
using namespace std;
main()
{
Planet wingwing;
wingwing.setPlanet();
cout<<"your weight will be "<<wingwing.otherPlanet()<<
" in "<<wingwing.returnplanet()<<'.'<<endl;
return 0;
}
请问为什么setPlanet老是出错的。 感激万分。