C++ Length类:unsigned int类型转换运算符及double构造器问题
问题:Length类的unsigned int类型转换运算符编译失败,转换构造函数存在错误
作业要求
- 为Length类编写unsigned int类型转换运算符:将结果设置为
len_inches对应十进制值的四舍五入结果 - 编写转换构造函数:实现从double类型创建Length对象
当前问题
已修复部分问题,但unsigned int类型转换运算符无法编译,只能使用普通int,不符合作业要求;同时转换构造函数存在逻辑错误。
相关代码
Length2.cpp
#include "Length2.h" //*********************************************** // Operator double converts Length to a double * //*********************************************** Length::operator double() const { return this->len_inches /12 + (this->len_inches %12) / 12.0; } //*********************************************** // Operator converts Length to a unsigned int * //*********************************************** Length::operator unsigned int() const { return round(this->len_inches / 12 + (this->len_inches % 12) / 12.0); } //********************************************* // Overloaded stream insertion operator << * //********************************************* ostream &operator<<(ostream& out, Length a) { out << a.getFeet() << " feet, " << a.getInches() << " inches"; return out; }
Length2.h
#pragma once #include <iostream> using namespace std; class Length { private: int len_inches; public: Length(int feet, int inches) { setLength(feet, inches); } Length(int inches){ len_inches = inches; } Length(double feet){ len_inches = feet * 144; } int getFeet() const{ return len_inches / 12; } int getInches() const { return len_inches % 12; } void setLength(int feet, int inches) { len_inches = 12 *feet + inches; } // Type conversion operators operator double() const; operator int() const; // Overloaded stream output operator friend ostream &operator<<(ostream &out, Length a); //Convert Constructor from double Length(double dValue) { value = dValue; } double getValue() const { return value; } };
main.cpp
// This program demonstrates the type conversion operators for // the Length class. #include "Length2.h" #include <iostream> #include <string> #include <fstream> //fstream, ifstream, ofstream #include <cmath> using namespace std; int main() { int ui = 0; Length distance(4, 6); ui = distance; cout << ui << " feet." << endl; Length distance1(4.5); double ui2 = -1; ui2 = distance1; cout << ui2 << " inches." << endl; return 0; }
解决方案
1. 修复unsigned int转换运算符编译问题
- 在
Length2.h的类型转换运算符声明中,添加unsigned int版本的声明:
// Type conversion operators operator double() const; operator int() const; operator unsigned int() const; // 新增此行
- 转换运算符实现中,将
round返回的double显式转换为unsigned int,同时简化计算逻辑:
Length::operator unsigned int() const { double total_feet = this->len_inches / 12.0; return static_cast<unsigned int>(round(total_feet)); }
2. 修复double转换构造函数问题
- 类中不存在
value成员变量,所有构造函数应操作核心成员len_inches - 合并重复的double参数构造函数,明确逻辑为将英尺数转换为总英寸数:
// 替换原有两个double参数构造函数 Length(double feet) { len_inches = static_cast<int>(round(feet * 12)); }
- 删除类中错误的
value相关代码(包括double getValue() const和构造函数内的value = dValue;)
3. 调整main函数测试逻辑
若要测试unsigned int转换运算符,需将对应变量类型改为unsigned int:
unsigned int ui = 0; Length distance(4, 6); ui = distance; cout << ui << " feet." << endl;
内容的提问来源于stack exchange,提问作者Anonymous
相关产品推荐
相关产品推荐

