C++ type qualifier problem
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) { re开发者_JAVA技巧turn _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.
You need to qualify your accessors as const too:
int GetNum(void) const { return _num; }
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.
You GetNum and GetDen accessor methods are not declared as "const", so they cannot be called within the operator+ against const Fractions.
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.
Your getters are not const:
int GetNum() const { return _num; }
int GetDen() const { return _den; }
/// ^^^^^^^^
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.
const Fraction Fraction::operator+ (const Fraction &rhs) const
{
return Fraction(_num * rhs._den+ (rhs._num * _den), _den * rhs._den);
}
Other Notes:
- Also setting void as the parameter is not a good idea (this is C style).
- const int has really no meaning just make it return int.
- Please don't use underscore as the first character. Its legal but if you are not careful and don't know all the rules about underscore it is easy to trip up.
const int GetDen(void) { return _den; }
should (or might) be
int GetDen(void) const { return _den; }
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.
精彩评论