2017-04-17 07:28

# C++main()函数里怎么样输出类中的变量？

bool CCarPark::cal()
{
double Rmin = Al / tan(Amax * PI / 180);
double D = 200; //预留宽度
double M = (Al + Ef) * (Al + Ef) + 2 * Rmin * Vm + (2 * Rmin - Vm) * D - D * D;

``````//求最小车位长
double SL = sqrt(M) + Er + D;
// 求theta角
double P = Vm / 2 - Rmin + D;
double Q = (Vm * D - 2 * Rmin * D + D * D + (Er + D - SL) * (Er + D - SL)) / (4 * Rmin) + Rmin;
double a = (SL - D - Er) * (SL - D - Er) + (Rmin - Vm / 2 - D) * (Rmin - Vm / 2 - D);
double b = 2 * (Er + D - SL) * Q;
double c = Q * Q - P * P;
double Ang = asin((-b - sqrt(b * b - 4 * a * c)) / (2 * a));       //与x轴的角度(弧度的表示形式)
double  theta = Ang * 180 / PI;                                     //转换为角度表示形式

//求起始点坐标，以车位左下角为圆心
static int x0 = 2 * Rmin * sin(Ang) + Er + D;                           //横坐标
static int y0 = 2 * Rmin - 2 * Rmin * cos(Ang) - Vm / 2 + Ws - D;       //纵坐标

//转换点坐标
int x1 = x0 - Rmin * sin(Ang);
int y1 = y0 - Rmin * (1 - cos(Ang));

//对车身轨迹进行描点
for (int i = 0; i <= 265; i++)
{
double N = 10 * i / Rmin;
m_pxTrack[i].x =  x0 - Rmin*sin(N);
m_pxTrack[i].y = Rmin*cos(N) + y0 - Rmin;
}

return true;
``````

}
int main()
{
CCarPark park(38,4475,1840,925,905,2645);
park.cal();
cout << SL << endl;
cout x0 << "," << y0 << endl;
system("pause");
return 0;
}

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• shen_wei 2017-04-17 08:53
已采纳
`````` class A
{
public:
void Test();
public:
int nX;
int nY;
};

void A::Test()
{
nX = 10;
nY = 20;
}

int main()
{

A TestA;
TestA.Test();
cout <<TestA.nX << " " <<TestA.nY;
｝
``````
点赞 评论
• lunhui2016 2017-04-17 07:47

把你要输出的变量定义成类的成员变量或全局变量，否则在栈中生命周期太短了

点赞 评论