This code won't run.. There is something wrong with "to_string"

#include <iostream>
using namespace std;
class Elevator
{
public: // Access specifier
int currentFloor; // Attribute (int variable) Current Floor number
bool upDirection; // Attribute (bool variable) flag for updirection
void move(int numFloors)
{ // Method/function to move between floors
if (upDirection == true)
{
currentFloor = currentFloor + numFloors;
}
else { currentFloor = currentFloor - numFloors; }
};
void stop() { // Method/function to stop
cout << "Elevator stoped" << endl;
};
string status()
{ // Method/function to move between floors
string str = to_string(currentFloor);
return str;
};
};
int main() { //Stack memory is set up by a simple variable declaration
Elevator stackObj; // Create an object of Elevator class and its stored in stack memory
// heap memory is set up through the use of the new keyword
Elevator* heapObj = new Elevator(); // Create an object of Elevator class and its stored in heap memory
cout << "Elevator move upward call using stack memory object " << endl;
stackObj.upDirection = true;
cout << "Move 3" << endl;
stackObj.move(3); // Call the method move by 3
cout << "set up direction downward" << endl;
stackObj.upDirection = false; cout << "Move 1" << endl;
stackObj.move(1);// Call the method move by 1
stackObj.stop(); cout << "checking status" << endl;
cout << "Elevator reached at " + stackObj.status() << endl;
cout << "Elevator move upward call using heap memory object " << endl;
heapObj->upDirection = true;
cout << "Move 3" << endl;
heapObj->move(3);; // Call the method move by 3
cout << "set up direction downward" << endl;
heapObj->upDirection = false;
cout << "Move 1" << endl;
heapObj->move(1); // Call the method move by 1
heapObj->stop();
cout << "checking status" << endl;
cout << "Elevator reached at " + heapObj->status() << endl;
}
I added a header include string and the code worked. but the output is confusing... how to organize?
One problem I see is that currentFloor is not initialized so it has a random value.
It needs formatting for readability.
https://www.cplusplus.com/articles/jEywvCM9/

Please edit and add code tags.
Line 14: You have a typo. * instead of =.

You really should have a default constructor to initialize the state of currentFloor and upDirection.

Encapsulation dictates that currentFloor and upDirection should be private.
Topic archived. No new replies allowed.