Josie0625 2019-07-15 14:19 采纳率: 0%
浏览 4052
已采纳

c++报错x was not declared in this scope 其中x是对象的名 求助一下为什么会报错呢

新手求轻喷......

#include

using namespace std;

class People
{
public:
People(int i,float j);
~People(){}
float calculateBMI(int myWeight,float myHeight);
void getAnswer();
private:
int myWeight;
float myHeight,myBMI;
};
People::People(int i,float j)
{
myWeight=i;
myHeight=j;
}
float People::calculateBMI(int myWeight,float myHeight)
{
float myBMI;
myBMI=myWeight/(myHeight*myHeight);
return myBMI;
}
void People::getAnswer()
{
if(myBMI>=18.5&&myBMI<=23.9)
cout<<"No!"<<endl;
else
cout<<"Yes!"<<endl;
}

int main()
{
int myWeight;
float myHeight;
while(cin>>myWeight>>myHeight)
People x(int myWeight,float myHeight);
x.calculateBMI(myWeight,myHeight);
x.getAnswer();
return 0;
}


  • 写回答

3条回答 默认 最新

  • adgentleman 2019-07-15 14:58
    关注

    1.因为你的x是在while循环定义的,所以当循环条件不成立的时候相当于x没有定义
    2.然后创建x的时候还有点问题,
    People x(int myWeight,float myHeight);
    参数只需要传变量名,改成
    People x(myWeight,myHeight);

    以下是我改的main函数,编译运行没问题
    int main()
    {
    int myWeight=1;
    float myHeight=1.5;
    // while(cin>>myWeight>>myHeight)
    People x(myWeight,myHeight);
    x.calculateBMI(myWeight,myHeight);
    x.getAnswer();
    return 0;
    }
    希望帮到你

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用