c++ - 第一次从现有的 main 编写头文件和 .cpp;无输出

标签 c++ visual-studio-2010 output

<分区>

这是我第一次为现有的主程序创建头文件和 cpp 文件。我可以让程序运行,但没有输出。如果有人可以帮助我解决问题,我将不胜感激。该程序应该模拟电梯。谢谢!

这是我得到的:

int main()

{

  elevator aLift(1);
 aLift.select(5);
  aLift.select(3);


  system("pause");
  return 0;
}

这是我的头文件。

#ifndef elevator_h   
#define elevator_h  
#include <string>    
using namespace std; 



class elevator {
public: //operations

elevator();
//coonstructors
elevator (int initFloor);

//modifiers
void select (int newFloor);
//my floor is increased/decreased by difference.

//accessors
int getFloor() const;
//gets current floor number



private: //state
    int my_floor;
    int selected_floor;



};
#endif   // ifndef elevator_h

最后,这是我的cpp文件

#include "elevator.h"
#include <string>
#include <iostream> 
using namespace std; 


int selected_floor;
elevator;
elevator::elevator (int initFloor)
//coonstructors
{
    my_floor=initFloor;


}

    //modifiers
    void elevator::select (int)
    {
        while(my_floor < selected_floor)
    cout << "Going up to " <<  ++my_floor << endl;
    }
    //my floor is increased/decreased by difference.

    //accessors
    int elevator::getFloor() const
    { 
        return selected_floor;
    }

最佳答案

全局变量初始化为0,见here .

因此您的 selected_floor 为 0 且小于 my_floor

编辑:

乍一看我错过了你有一个成员 selected_floor 和一个全局成员。我认为您不需要全局的。

你缺少的是:

// don't forget the default constructor
elevator::elevator ()
: my_floor(0)
, selected_floor(0)
{}

elevator::elevator (int initFloor)
: my_floor(initFloor)
, selected_floor(0)
{}

//modifiers
void elevator::select (int newFloor)
                         //^^^^^^^^
{
    selected_floor = newFloor;  // set you selected_floor to the value you want to select
  //^^^^^^^^^^^^^^^^^^^^^^^^^^
    while(my_floor < selected_floor)
    cout << "Going up to " <<  ++my_floor << endl;
}

关于c++ - 第一次从现有的 main 编写头文件和 .cpp;无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929410/

相关文章:

c++ - 如何在不更改所有代码以使用 this-> 的情况下避免旧模板重库中的 'not declared' 错误?

c++ - wxBitmapButton 读取类参数

c++ - 无法在 Windows 下以正常的方式使用 gmp 和 uint64_t

c# - Visual Studio 类图查看器独立

javascript - 使用 javascript 取消表单提交不起作用

c++ - 跨多个操作系统联网

c# - Windows 窗体 - 从按钮类型的对象中获取文本值

vb.net - 为什么我无法在 vb.net Visual Studio 2010 中声明应用程序事件?

c - Hackerrank 中的 C 有什么不同(得到不同的输出)吗?

c - 两个格式说明符但只有一个参数