C++类型限定符问题
作为我的计算机软件开发学位的一部分,我的实验室之一包括创建计算器类模板和分数类。
问题出在我的分数课上。我现在的任务是重载加号运算符,以允许两个分数相加。
Fraction.cpp:
#include "Fraction.h"
const Fraction Fraction::operator+ (const Fraction &rhs) const
{
return Fraction(_num * rhs.GetDen() + (rhs.GetNum() * _den), _den * rhs.GetDen());
}
Fraction.h
#pragma once
class Fraction
{
public:
Fraction(const int &num, const int &den) : _num(num), _den(den) {}
~Fraction(void) {}
const int GetNum(void) { return _num; }
const int GetDen(void) { return _den; }
const Fraction operator+ (const Fraction &rhs) const;
private:
const int _num, _den;
};
Visual Studio 抱怨我的分数访问器无法“将此指针从 const 分数转换为分数 &”。我完全困惑了。
As part of my Computer Software Development degree, one of my labs consists of creating a calculator class template and a fraction class.
The problem is with my fraction class. My task is now to overload the plus operator to allow two fractions to be added together.
Fraction.cpp:
#include "Fraction.h"
const Fraction Fraction::operator+ (const Fraction &rhs) const
{
return Fraction(_num * rhs.GetDen() + (rhs.GetNum() * _den), _den * rhs.GetDen());
}
Fraction.h
#pragma once
class Fraction
{
public:
Fraction(const int &num, const int &den) : _num(num), _den(den) {}
~Fraction(void) {}
const int GetNum(void) { return _num; }
const int GetDen(void) { return _den; }
const Fraction operator+ (const Fraction &rhs) const;
private:
const int _num, _den;
};
Visual Studio complains that my fraction accessors cannot 'convert this pointer from const fraction to fraction &'. I'm completely baffled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还需要将访问器限定为 const:
顺便说一句,将返回类型限定为 const int 并没有真正意义,无论如何它都是一个 int。您的编译器应该发出警告。
You need to qualify your accessors as const too:
BTW, qualifying the return type as const int does not really make sense, it will be an int anyway. Your compiler should emit a warning.
GetNum 和 GetDen 访问器方法未声明为“const”,因此无法在针对 const 分数的运算符+ 中调用它们。
You GetNum and GetDen accessor methods are not declared as "const", so they cannot be called within the operator+ against const Fractions.
第一个问题,为什么你让函数首先返回一个“const Fraction”?这对代码的使用方式施加了任意限制。很明显,您的 Fraction 类基本上已经是隐式 const 了。无需这样做。
但是,您的成员函数是非常量的。也就是说,它们不会告诉编译器您不会尝试更改类本身的状态。对于所有不改变状态的成员函数,请将关键字“const”放在成员函数声明的末尾。
First question, why are you having the function return a "const Fraction" in the first place? That puts an arbitrary restriction on how your code can be used. It's quite clear that your Fraction class is already by and large implicitly const. No need to do so.
However, your member functions are non-const. That is, they don't tell the compiler that you won't be attempting to change state within the class itself. Please put the keyword "const" at the end of the member function declarations for all member functions which don't change state.
你的吸气剂不是 const:
你实际上并不需要这些吸气剂。就我个人而言,我会把它们扔掉。
注意:类是其自身的友元,因此可以访问另一个实例的成员。
其他注意事项:
Your getters are not const:
You don't actually need these getters. Personally I would throw them away.
Note: That a class is a friend of itself and thus can access members of another instance.
Other Notes:
应该(或可能)是
第一个返回 const 副本。通过复制返回意味着将生成副本并将其作为临时对象(因此是 const(“只读”)对象)提供给调用者函数。因此通过副本返回会使副本变为只读。
第二个使你的函数可以调用,即使 Fraction 对象是 const - 它是一个“只读”可访问函数(你可能会说)。这可能是您想要编写的内容,如果不是,也比没有 const 更好。
顺便说一下,参数中的 void 在 C++ 中没有用,您可以避免键入它,只需说
int GetNum() const{ return _num; }
另一个细节:以下划线字符开头的名称由标准保留用于标准实现。使用它们并不是很危险,但你永远不知道。如果你想保留它们,至少将所有代码封装在命名空间中。
should (or might) be
The first one returns a const copy. A return by copy imply that the copy will be generated and provided to the caller function as a temporary object, therefore a const ("read-only") object. So returning by copy makes the copy read-only.
The second makes your function being callable even if the Fraction object is const - it's a "read-only" accessible function (you might say). It's probably what you wanted to write, and if not, it's better than without the const.
By the way, the void in parameters isn't useful in C++, you can avoid typing it and just say
int GetNum() const{ return _num; }
Another detail : names starting by underscore caracter are reserved by the standard for standard implementations. It's not very dangerous to use them, but you never know. If you want to keep them, at least encapsulate all your code in namespace.