Weird error no operator found?
Nov 23, 2013 at 4:06pm UTC
Hello all, so I'm working on some code with an instructor given driver program, but I'm coming across two errors one of which is apparently popping up in the instructor given code and another which is telling me that
no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
and a verrrry long error message after the fact, any insight?? I've emailed the instructor repeatedly throughout the week, but have yet to receive a reply (its an online course). This code is going off a previous assignment received which he is having us modify for this assignment.
My .h file
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
//DollarsCents class definition
class DollarsCents
{
//friend classes
friend std::istream& operator >>(std::istream &in, const DollarsCents& dcRef);
friend std::ostream& operator <<(std::ostream &out, const DollarsCents& dcRef);
//variables
private :
int dollars;
int cents;
void simplify()
{
while (cents >= 100)
{
cents-=100;
dollars++;
}
}
//functions
public :
DollarsCents();
DollarsCents(int numDollars, int numCents);
int getDollars();
int getCents();
DollarsCents operator +(DollarsCents& dcRef);
DollarsCents operator -(DollarsCents& dcRef);
DollarsCents operator /(const int num);
DollarsCents operator *(const int num);
DollarsCents& operator +=(DollarsCents& dcRef);
DollarsCents& operator -=(DollarsCents& dcRef);
DollarsCents& operator /=(const int num);
DollarsCents& operator *=(const int num);
bool operator <(DollarsCents& dcRef);
bool operator <=(DollarsCents& dcRef);
bool operator >(DollarsCents& dcRef);
bool operator >=(DollarsCents& dcRef);
bool operator ==(DollarsCents& dcRef);
bool operator !=(DollarsCents& dcRef);
};
My .cpp file
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h"
using namespace std;
//friend functions
istream& operator >>(istream &in, const DollarsCents& dcRef)
{
char dot;
in >> dcRef.dollars >> dot >> dcRef.cents;
return in;
}
ostream& operator <<(std::ostream &out, const DollarsCents& dcRef)
{
out << "$" << dcRef.dollars << "." << dcRef.cents;
return out;
}
//default constructor
DollarsCents::DollarsCents()
{
dollars = 0;
cents = 0;
}
//Two-parameter constructor
DollarsCents::DollarsCents(int numDollars, int numCents)
{
dollars = numDollars;
cents = numCents;
if (cents < 0)
{
cents = 0;
}
simplify();
}
//Get Functions
int DollarsCents::getDollars()
{
return dollars;
}
int DollarsCents::getCents()
{
return cents;
}
//Calculation Functions
DollarsCents DollarsCents::operator +(DollarsCents& dcRef)
{
DollarsCents dc_add;
dc_add.dollars = dollars + dcRef.dollars;
dc_add.cents = cents + dcRef.cents;
return dc_add;
}
DollarsCents DollarsCents::operator -(DollarsCents& dcRef)
{
DollarsCents dc_subtract;
if (dcRef.cents > cents)
{
dc_subtract.dollars = (dollars - 1) - dcRef.dollars;
dc_subtract.cents = (cents + 100) - dcRef.cents;
}
else
{
dc_subtract.dollars = dollars - dcRef.dollars;
dc_subtract.cents = cents - dcRef.cents;
}
return dc_subtract;
}
DollarsCents DollarsCents::operator /(const int num)
{
int totalCents = (dollars*100) + cents;
totalCents /= num;
DollarsCents dc_divide(0, totalCents);
dc_divide.simplify();
return dc_divide;
}
DollarsCents DollarsCents::operator *(const int num)
{
int totalCents = (dollars*100) + cents;
totalCents *= num;
DollarsCents dc_multiply(0, totalCents);
dc_multiply.simplify();
return dc_multiply;
}
DollarsCents& DollarsCents::operator +=(DollarsCents& dcRef)
{
DollarsCents dc;
dcRef += dc;
return *this ;
}
DollarsCents& DollarsCents::operator -=(DollarsCents& dcRef)
{
DollarsCents dc;
dcRef -= dc;
return *this ;
}
DollarsCents& DollarsCents::operator /=(const int num)
{
DollarsCents dc;
dc /= num;
return *this ;
}
DollarsCents& DollarsCents::operator *=(const int num)
{
DollarsCents dc;
dc *= num;
return *this ;
}
//bool functions
bool DollarsCents::operator <(DollarsCents& dcRef)
{
return (dollars < dcRef.dollars && cents < dcRef.dollars && cents);
}
bool DollarsCents::operator <=(DollarsCents& dcRef)
{
return (dollars <= dcRef.dollars && cents <= dcRef.dollars && cents);
}
bool DollarsCents::operator >(DollarsCents& dcRef)
{
return (dollars > dcRef.dollars && cents > dcRef.dollars && cents);
}
bool DollarsCents::operator >=(DollarsCents& dcRef)
{
return (dollars >= dcRef.dollars && cents >= dcRef.dollars && cents);
}
bool DollarsCents::operator ==(DollarsCents& dcRef)
{
return (dollars == dcRef.dollars && cents == dcRef.dollars && cents);
}
bool DollarsCents::operator !=(DollarsCents& dcRef)
{
return (dollars != dcRef.dollars && cents != dcRef.dollars && cents);
}
Nov 23, 2013 at 4:24pm UTC
Nov 23, 2013 at 4:39pm UTC
8 9 10 11 12 13 14 15 16 17 18
istream& operator >>(istream &in, const DollarsCents& dcRef)
{
char dot;
in >> dcRef.dollars >> dot >> dcRef.cents;
cin >> dot;
return in;
}
dcRef is declared as const so you can't change it in your function
Last edited on Nov 23, 2013 at 5:48pm UTC
Nov 23, 2013 at 5:38pm UTC
Sigh indeed, it appears according to other students in the class he gave us an incorrect file so gonna have to start over...
Topic archived. No new replies allowed.