Я работаю над программой, которая вычисляет объем окна, используя классы, чтобы помочь мне понять, как использовать классы. В рамках программы я хочу преобразовать объекты, длину и ширину объектов в строку, чтобы отобразить размеры окна. Когда я запускаю код из моего основного файла, он сбой. Когда я запускаю его из файла класса я получаю сообщение «не удалось преобразовать Box :: длину от двойной STD :: строка. Как я могу исправить ошибку преобразования?Не удалось преобразовать double в строку
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
class Box
{
public:
double length;//length of the box
double height;//height of the box
double width;//with of the box
Box(): length(1), height(1), width(1){}
//Parameterized Constructor
Box(double length, double width, double height);
double getVolume(void);
//Mutators
void setLength(double leng);
void setWidth(double wid);
void setHeight(double hei);
//Acessors
string toString() const;
string getLength();
string getWidth();
string getHeight();
};//end class
//member function definitions
double Box::getVolume(void)//get volume will cal and output the volume when called
{
return length * width * height;
}
void Box::setLength(double leng)
{
const double MIN_LENGTH = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_LENGTH = 99;
if (length > MAX_LENGTH || length < MIN_LENGTH)
{
stringstream strOut;//declare string stream
strOut << "Length is out of range. Length must be between" << MIN_LENGTH << " and " << MAX_LENGTH << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
length = leng;// if length is within range, store it
}
}
string Box::getLength()
{
return length;
}
void Box::setWidth(double wid)
{
const double MIN_WIDTH = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_WIDTH = 99;
if (length > MAX_WIDTH || length < MIN_WIDTH)
{
stringstream strOut;//declare string stream
strOut << "Width is out of range. Width must be between" << MIN_WIDTH << " and " << MAX_WIDTH << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
width = wid;// width is in range, store it
}
}
string Box::getWidth()
{
return width;
}
void Box::setHeight(double hei)
{
const double MIN_HEIGHT = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_HEIGHT = 99;
if (length > MAX_HEIGHT || length < MIN_HEIGHT)
{
stringstream strOut;//declare string stream
strOut << "Height is out of range. Height must be between" << MIN_HEIGHT << " and " << MAX_HEIGHT << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
height = hei;// height is in range, store it
}
}
string Box::getHeight()
{
return height;
}
string Box::toString() const
{
stringstream strOut;
strOut << "Length: " << getLength() << endl
<< "Width: " << getWidth() << endl <<
"Height: " << getHeight() << endl;
return strOut;
}
что? это бессмысленно. Вы не можете запустить код из файла класса. «не удалось преобразовать Box :: length из double в std :: string», возможно, не будет сообщением об ошибке при запуске. Похоже на ошибку компилятора типа mismatch, которая говорит что-то вроде «не может преобразовать Box :: length из double в std :: string». – thang
did not mean run, означало компиляцию, чтобы увидеть, что ошибок не было – Ryan
Не выводить ошибку в строку, где значение будет 'strOut <<" Height is out ... ... ". Для этого нужны исключения. –