Yeah, I messed up on the arithmetic operator definitions. My bad there. But for those functions not taking an Int object as an argument: do they need to? I mean, they already have the implicit this pointer since they're member functions of the class.
Int(const int)
is a normal constructor and
Int(const int&)
is (meant to be) a copy constructor. And the
Int(const int&&)
is supposed to be a move constructor. I need to know the correct ways to declare and define them for this class if they're wrong.
@Repeater: The >> and << operators aren't member or friend functions, so I can't directly access m_value in them since it's a private member. That's what the getter member function is for.
Edit: Also, @doug4: Do I have to take out the reference operator from the assignment operators as well, or only from the arithmetic operators?
I'm thinking I should also define compound assignment operators for this class, by the way.
When I try to do:
1 2 3 4 5 6 7 8
|
std::istream &operator>>(std::istream &is, Int &val)
{
if (is)
{
is >> val.getInt();
}
return is;
}
|
Even after defining a completely non-const version of getInt(), I still get the error message,
no operator ">>" matches these operands
operand types are: std::istream >> int |
What's going on?
std::istream >> int
is supposed to be valid, isn't it? What did I do wrong? Should I make a friend of the class, so I can just m_value, after all?
Edit2: I made the >> operator a friend function. Now it's like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
// Osman Zakir
// 10 / 25 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 19 Exercise 5
// Exercise Specifications:
/**
* Define a class Int having a single member of class int . Define construc-
* tors, assignment, and operators + , – , * , / for it. Test it, and improve its
* design as needed (e.g., define operators << and >> for convenient I/O).
*/
#include <iostream>
#include <algorithm>
#include "../../cust_std_lib_facilities.h"
class Int
{
public:
Int(const int value = 0)
: m_value{ value } { }
Int operator+(const int value);
Int operator-(const int value);
Int operator*(const int value);
Int operator/(const int value);
Int &operator=(const int value);
Int &operator=(const int &value);
Int &operator=(const int &&value);
int getInt() const { return m_value; }
int getInt() { return m_value; }
friend std::istream &operator>>(std::istream &is, Int &val);
private:
int m_value;
};
std::ostream &operator<<(std::ostream &os, const Int &val);
int main()
{
Int myInt{ 5 };
std::cout << myInt << '\n';
myInt = myInt + 4;
std::cout << myInt << '\n';
std::cout << "Please give me an int: ";
Int myInt2;
std::cin >> myInt2;
std::cout << "You entered: " << myInt2 << '\n';
keep_window_open();
}
Int Int::operator+(const int value)
{
int result = m_value + value;
return Int(result);
}
Int Int::operator-(const int value)
{
int result = m_value - value;
return Int(result);
}
Int Int::operator*(const int value)
{
int result = m_value * value;
return Int(result);
}
Int Int::operator/(const int value)
{
int result = m_value / value;
return Int(result);
}
Int &Int::operator=(const int value)
{
m_value = value;
return *this;
}
Int &Int::operator=(const int &value)
{
m_value = value;
return *this;
}
Int &Int::operator=(const int &&value)
{
m_value = value;
return *this;
}
std::ostream &operator<<(std::ostream &os, const Int &val)
{
if (os)
{
os << val.getInt();
}
return os;
}
std::istream &operator>>(std::istream &is, Int &val)
{
if (is)
{
is >> val.m_value;
}
return is;
}
|
And it compiles and runs correctly (or at least seems to, for now).