Nov 4, 2014 at 7:38pm UTC
Hello all I am new to C++ and I am having a few errors when compiling my code for class. The point of the program is to overload the different operators. The errors I am getting I have never seen before. Any pointers to where my issue may be would help.
errors are as followed:
main.cpp|16|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'fractionType()'
main.cpp|19|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'fractionType()'
main.cpp|23|error: invalid operands of types 'fractionType()' and 'fractionType()' to binary 'operator+'
main.cpp|25|error: ISO C++ forbids using pointer to a function in subtraction [-fpermissive]
main.cpp|27|error: invalid operands of types 'fractionType()' and 'fractionType()' to binary 'operator*'
main.cpp|29|error: invalid operands of types 'fractionType()' and 'fractionType()' to binary 'operator/'
here is my main.cpp
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "fractionType.h"
using namespace std;
int main()
{
cout <<"\n\n\tThis program checks the functionality"
<<"\n\tof the fractionType. \n" ;
fractionType myFraction1();
fractionType myFraction2();
cout << "\n\tEnter a fraction following the form a/b :" ;
cin >> myFraction1;
cout << "\n\tEnter another fraction following the form a/b :" ;
cin >> myFraction2;
//Testing math overloaders
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 + myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 - myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 * myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 / myFraction2;
//Testing the realationship overloaders
cout << "\n\t" << myFraction1 << "==" << myFraction2;
if ( myFraction1 == myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "!=" << myFraction2;
if ( myFraction1 != myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "<" << myFraction2;
if ( myFraction1 < myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "<=" << myFraction2;
if ( myFraction1 <= myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << ">" << myFraction2;
if ( myFraction1 > myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << ">=" << myFraction2;
if ( myFraction1 >= myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\n\t" ;
system("pause" );
return 0;
}
fractionOverloading.cpp
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
#include <iostream>
#include <iomanip>
#include "fractionType.h"
using namespace std;
//constructors
fractionType::fractionType(const int & nu, const int & de)
{
a = nu;
if (de==0)
{
cout <<"\n\tInvalid denominator."
<<"default value considered for denominator." ;
b = 1;
}
else
b = de;
}
//default Constructor
fractionType::fractionType()
{
a = 0;
b = 1;
}
//copy Constructor
fractionType::fractionType(const fractionType& rightFraction)
{
a = rightFraction.a;
b = rightFraction.b;
}
//destructor
fractionType::~fractionType()
{
}
//overloading relational operators
bool fractionType::operator == (const fractionType& rightFraction) const
{
return (( a == rightFraction.a)&& (b == rightFraction.b));
}
bool fractionType::operator != (const fractionType& rightFraction) const
{
return (( a != rightFraction.a)|| (b != rightFraction.b));
}
bool fractionType::operator < (const fractionType& rightFraction) const
{
return (a * rightFraction.b < b * rightFraction.a);
}
bool fractionType::operator <= (const fractionType& rightFraction) const
{
return ( a * rightFraction.b <= b* rightFraction.a);
}
bool fractionType::operator > (const fractionType& rightFraction) const
{
return ( a * rightFraction.b > b* rightFraction.a);
}
bool fractionType::operator >= (const fractionType& rightFraction) const
{
return ( a * rightFraction.b >= b* rightFraction.a);
}
bool fractionType fractionType::operator + (const fractionType& rightFraction);
{
fractionType temp;
temp.a = (a* rightFraction.b)+(b* rightFraction.a)
temp.b = b * rightFraction.b;
return temp;
}
bool fractionType fractionType::operator - (const fractionType& rightFraction);
{
fractionType temp;
temp.a = (a* rightFraction.b)-(b* rightFraction.a)
temp.b = b * rightFraction.b;
return temp;
}
bool fractionType fractionType::operator * (const fractionType& rightFraction);
{
fractionType temp;
temp.a = a * rightFraction.a;
temp.b = b * rightFraction.b;
return temp;
}
bool fractionType fractionType::operator / (const fractionType& rightFraction);
{
fractionType temp;
//Checking to see if division is legal
if ((rightFraction.a == 0)|| (rightFraction.b==0))
{
temp.a=0;
temp.b=1;
cout << "\n|tInvalid to perform division." ;
}
else
{
temp.a = a * rightFraction.b;
temp.b = b * rightFractin.a;
}
return temp;
}
ostream& operator << (ostream& osObject, const fractionType& myFraction)
{
osObject << myFraction.a << "/" << myFraction.b;
return osObject;
}
istream& operator >> (istream& isObject, const fractionType& myFraction)
{
char ch;
isObject >> myFraction.a >> ch >> myFraction.b;
return isObject;
}
fractionType.h
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
#ifndef FRACTIONTYPE_H_INCLUDED
#define FRACTIONTYPE_H_INCLUDED
#include <iostream>
using namespace std;
class fractionType
{
friend ostream& operator << (ostream&, const fractionType&);
friend istream& operator >> (istream&, fractionType&);
public :
//overloading operators
const fractionType& operator =(const fractionType&);
//constructors
fractionType();
fractionType(const fractionType&);
fractionType(const int &, const int &);
//destructor
~fractionType();
bool operator == (const fractionType&) const ;
bool operator != (const fractionType&) const ;
bool operator <= (const fractionType&) const ;
bool operator < (const fractionType&) const ;
bool operator >= (const fractionType&) const ;
bool operator > (const fractionType&) const ;
fractionType operator +(const fractionType&);
fractionType operator -(const fractionType&);
fractionType operator *(const fractionType&);
fractionType operator /(const fractionType&);
private :
int a;
int b;
};
#endif // FRACTIONTYPE_H_INCLUDED
Last edited on Nov 4, 2014 at 7:39pm UTC
Nov 5, 2014 at 6:24am UTC
3 corrections
1.remove ; in lines 66,75,84,93 from fractionOverloading.cpp
2. Correct return type from "bool fractionType " to "fractionType" type in operator + and operator -
3. The following errors are due to lines 12,13 in main.cpp
rrors are as followed:
main.cpp|16|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'fractionType()'
main.cpp|19|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'fractionType()'
Change from
fractionType myFraction1();
fractionType myFraction2();
to simply
fractionType myFraction1 ;
fractionType myFraction2 ;
I just compiled in my systems and no errors are coming ...
Nov 5, 2014 at 4:20pm UTC
I did this and compiled again and got an error now in my fractionOverloading.cpp line 120.
error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'const int')
Nov 5, 2014 at 8:59pm UTC
there are minor errors like missing ; , having const qualifier in operator>> argument etc.
only operator << will have const arg
pasting all
$ cat main.cpp
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "fractionType.h"
using namespace std;
int main()
{
cout <<"\n\n\tThis program checks the functionality"
<<"\n\tof the fractionType. \n" ;
fractionType myFraction1;
fractionType myFraction2;
cout << "\n\tEnter a fraction following the form a/b :" ;
cin >> myFraction1;
cout << "\n\tEnter another fraction following the form a/b :" ;
cin >> myFraction2;
//Testing math overloaders
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 + myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 - myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 * myFraction2;
cout << "\n\n\t" << myFraction1 << "+" << myFraction2
<< "=" << myFraction1 / myFraction2;
//Testing the realationship overloaders
cout << "\n\t" << myFraction1 << "==" << myFraction2;
if ( myFraction1 == myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "!=" << myFraction2;
if ( myFraction1 != myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "<" << myFraction2;
if ( myFraction1 < myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << "<=" << myFraction2;
if ( myFraction1 <= myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << ">" << myFraction2;
if ( myFraction1 > myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\t" << myFraction1 << ">=" << myFraction2;
if ( myFraction1 >= myFraction2 )
cout<<" is true." ;
else
cout <<" is false." ;
cout << "\n\n\t" ;
system("pause" );
return 0;
}
Last edited on Nov 5, 2014 at 11:19pm UTC
Nov 5, 2014 at 9:00pm UTC
$ cat fractionOverloading.cpp
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
#include <iostream>
#include <iomanip>
#include "fractionType.h"
using namespace std;
//constructors
fractionType::fractionType(const int & nu, const int & de)
{
a = nu;
if (de==0)
{
cout <<"\n\tInvalid denominator."
<<"default value considered for denominator." ;
b = 1;
}
else
b = de;
}
//default Constructor
fractionType::fractionType()
{
a = 0;
b = 1;
}
//copy Constructor
fractionType::fractionType(const fractionType& rightFraction)
{
a = rightFraction.a;
b = rightFraction.b;
}
//destructor
fractionType::~fractionType()
{
}
//overloading relational operators
bool fractionType::operator == (const fractionType& rightFraction) const
{
return (( a == rightFraction.a)&& (b == rightFraction.b));
}
bool fractionType::operator != (const fractionType& rightFraction) const
{
return (( a != rightFraction.a)|| (b != rightFraction.b));
}
bool fractionType::operator < (const fractionType& rightFraction) const
{
return (a * rightFraction.b < b * rightFraction.a);
}
bool fractionType::operator <= (const fractionType& rightFraction) const
{
return ( a * rightFraction.b <= b* rightFraction.a);
}
bool fractionType::operator > (const fractionType& rightFraction) const
{
return ( a * rightFraction.b > b* rightFraction.a);
}
bool fractionType::operator >= (const fractionType& rightFraction) const
{
return ( a * rightFraction.b >= b* rightFraction.a);
}
fractionType fractionType::operator + (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a* rightFraction.b)+(b* rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator - (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a* rightFraction.b)-(b* rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator * (const fractionType& rightFraction)
{
fractionType temp;
temp.a = a * rightFraction.a;
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator / (const fractionType& rightFraction)
{
fractionType temp;
//Checking to see if division is legal
if ((rightFraction.a == 0)|| (rightFraction.b==0))
{
temp.a=0;
temp.b=1;
cout << "\n|tInvalid to perform division." ;
}
else
{
temp.a = a * rightFraction.b;
temp.b = b * rightFraction.a;
}
return temp;
}
ostream& operator << (ostream& osObject, const fractionType& myFraction)
{
osObject << myFraction.a << "/" << myFraction.b;
return osObject;
}
istream& operator >> (istream& isObject, fractionType& myFraction)
{
char ch;
isObject >> myFraction.a >> ch >> myFraction.b;
return isObject;
}
Last edited on Nov 5, 2014 at 11:18pm UTC
Nov 5, 2014 at 9:00pm UTC
$ cat fractionType.h
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
#ifndef FRACTIONTYPE_H_INCLUDED
#define FRACTIONTYPE_H_INCLUDED
#include <iostream>
using namespace std;
class fractionType
{
friend ostream& operator << (ostream&, const fractionType&);
friend istream& operator >> (istream&, fractionType&);
public :
//overloading operators
const fractionType& operator =(const fractionType&);
//constructors
fractionType();
fractionType(const fractionType&);
fractionType(const int &, const int &);
//destructor
~fractionType();
bool operator == (const fractionType&) const ;
bool operator != (const fractionType&) const ;
bool operator <= (const fractionType&) const ;
bool operator < (const fractionType&) const ;
bool operator >= (const fractionType&) const ;
bool operator > (const fractionType&) const ;
fractionType operator +(const fractionType&);
fractionType operator -(const fractionType&);
fractionType operator *(const fractionType&);
fractionType operator /(const fractionType&);
private :
int a;
int b;
};
#endif // FRACTIONTYPE_H_INCLUDED
Last edited on Nov 5, 2014 at 11:18pm UTC