[英]User defined type conversions in C++
I'm new to C++我是 C++ 新手
below is the code for converting object of english distance(feet' inches") to meters and vice versa下面是将英制距离(英尺'英寸”)的对象转换为米的代码,反之亦然
#include <iostream>
using namespace std;
class Distance
{
private:
const float MTF;
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0), MTF(3.280833F) //no argument constructor
{ }
Distance(float meters) : MTF(3.28033F)//(1-arg constructor)
{//coverting metres to distance object
float fltfeet = MTF * meters;
feet = int(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in) : feet(ft), inches(in), MTF(3.280833F)
{ }
void getdist()//get distance from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const // o/p the distance
{ cout << feet << "\'-" << inches << '\"'; }
operator float() const //conversion operator
{ // converts distance to meters
float fracfeet = inches/12;
fracfeet += static_cast<float>(feet);
return fracfeet/MTF;
}
};
int main()
{
float mtrs;
Distance dist1 = 2.35F; //meters to distance
cout << "\ndist1 = "; dist1.showdist();
mtrs = static_cast<float>(dist1); //casting distance to meters
cout << "\ndist1 = " << mtrs << " meters\n";
Distance dist2(5, 10.25);
mtrs = dist2; //casting dist2 to meters
cout << "\ndist2 = " << mtrs << " meters\n";
Distance dist3; //new object dist3
dist3 = mtrs; //here is the error
//not converting meters to distance object
cout<<"\ndist3 = ";dist3.showdist();
return 0;
}
but the code shows the error :但代码显示错误:
In member function 'Distance& Distance::operator=(const Distance&)':
在成员函数 'Distance& Distance::operator=(const Distance&)' 中:
error: non-static const member 'const float Distance::MTF', can't use default assignment operator
错误:非静态常量成员 'const float Distance::MTF',不能使用默认赋值运算符
should'nt it be converting mtrs to object dist3 ?不应该将 mtrs 转换为对象 dist3 吗?
why error occurs?为什么会出现错误?
You error is actually with the line您的错误实际上与该行有关
dist3 = mtrs;
not不是
Distance dist3;
The reason for this is Distance
has a const
member variable.这样做的原因是
Distance
有一个const
成员变量。 Since it is const
it cannot be assigned to which cause the default copy assignment and move assignment operators to be deleted.由于它是
const
它不能被分配到导致默认的复制赋值和移动赋值运算符被删除。
You are going to have to rethink your design if you want to allow assignment of your objects or write your own custom assignment functions.如果您想允许分配对象或编写自己的自定义分配函数,您将不得不重新考虑您的设计。
If you want to have class scope constant, you better change your class definition to:如果您希望类作用域保持不变,最好将类定义更改为:
class Distance
{
private:
static constexpr float MTF = 3.280833F;
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0)
...
This will eliminate error you get and will work as you intended.这将消除您得到的错误并按您的预期工作。 Plus you do not have to define that constant value multiple times as you have in your code.
另外,您不必像在代码中那样多次定义该常量值。
Note: if you cannot use C++11 you can make MTF
global constant (better in unnamed namespace inside cpp file) or just static member.注意:如果您不能使用 C++11,您可以使
MTF
全局常量(在 cpp 文件中的未命名命名空间中更好)或只是静态成员。 Either way it will eliminate the error and you will need to define it only once, which is less error prone.无论哪种方式,它都会消除错误,您只需要定义一次,这样就不容易出错。
You have to override assignment operator.您必须覆盖赋值运算符。 And the code should be as shown below
代码应该如下所示
#include <iostream>
using namespace std;
class Distance
{
private:
const float MTF;
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0), MTF(3.280833F)
{ }
Distance(float meters) : MTF(3.28033F)
{
float fltfeet = MTF * meters;
feet = int(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in) : feet(ft), inches(in), MTF(3.280833F)
{ }
void getdist()
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const
{ cout << feet << "\'-" << inches << '\"'; }
operator float() const
{
float fracfeet = inches/12;
fracfeet += static_cast<float>(feet);
return fracfeet/MTF;
}
Distance& operator=(const Distance & otherD)
{
feet = otherD.feet;
inches = otherD.inches;
return *this;
}
};
int main()
{
float mtrs;
Distance dist1 = 2.35F;
cout << "\ndist1 = "; dist1.showdist();
mtrs = static_cast<float>(dist1);
cout << "\ndist1 = " << mtrs << " meters\n";
Distance dist2(5, 10.25);
mtrs = dist2;
cout << "\ndist2 = " << mtrs << " meters\n";
Distance dist3; //here is the error
dist3 = (Distance)mtrs ;
//cout<<"\ndist3 = ";dist3.showdist();
return 0;
}
Like another user said, you have a "const" variable, so you have to override assigment operator to address your requirements.就像另一个用户说的,你有一个“const”变量,所以你必须覆盖赋值运算符来满足你的要求。
I gather that the error comes at the line我认为错误来自该行
dist3 = mtrs;
The problem here is that Distance
does not have an assignment operator that takes an argument of type float
, so the compiler tries to create a temporary object of type Distance
and construct it with the argument mtrs
;这里的问题是
Distance
没有一个赋值运算符,它接受一个float
类型的参数,所以编译器尝试创建一个Distance
类型的临时对象并用参数mtrs
构造它; that's okay, but the next step is to assign that temporary value to dist3
, and the compiler is complaining that it can't assign the value of MTF
in the temporary object to the value of MTF
in dist3
because MTF
is const.没关系,但下一步是到该临时值赋给
dist3
,编译器抱怨它不能分配的值MTF
在临时对象的值MTF
在dist3
因为MTF
是常量。
Because of that const
, objects of type Distance
cannot be assigned.由于该
const
,无法分配Distance
类型的对象。 So, for example, dist3 = dist2
would also fail.因此,例如,
dist3 = dist2
也会失败。 That's probably not what you intended, and you can fix this by adding an assignment operator that simply ignores the value of MTF
.这可能不是您想要的,您可以通过添加一个简单地忽略
MTF
值的赋值运算符来解决这个问题。
The error occurs, because you could not declare const
inside a class.发生错误,因为您无法在类中声明
const
。 You should define the const variable outside the class.您应该在类之外定义 const 变量。 You should replace
const float MTF
with float MTF
here.你应该在这里用
float MTF
替换const float MTF
。
As stated in other answers, the issue is that you have declared the MTF
variable as const
.正如其他答案中所述,问题在于您已将
MTF
变量声明为const
。 There are ways around this though.有办法解决这个问题。 You've set the variable to be const because it's a constant and shouldnt change.
您已将变量设置为 const,因为它是一个常量,不应更改。 Instead, add a dedicated
Distance& operator=(float feet)
Method in which you actually set the feet and inches variable when passed in a float value:相反,添加一个专用的
Distance& operator=(float feet)
方法,在该方法中您在传入浮点值时实际设置英尺和英寸变量:
class Distance
{
private:
/* ... */
public:
/* ... */
Distance& operator=(float feet)
{
// Set the feet and inches here from
// the passed feet variable
return *this;
}
};
That should solve the problem of assigning the variable from a float.这应该可以解决从浮点数分配变量的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.