0

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. What am I doing wrong? Thank you

errors are as followed:

main.cpp|16|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'fractionType()'

main.cpp|19|error: ambiguous overload for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' 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 code,

main.cpp

#include <iostream>
#include <iomanip>
#include "fractionType.h"
using namespace std;

int main()
{
    fractionType num1(5, 6);
    fractionType num2();
    fractionType num3();

    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);

    cout << "Line 7: Num1 = " << num1 << endl;
    cout << "Line 8: Num2 = " << num2 << endl;

    cout << "Line 9: Enter the fraction "
         << "in the form a / b:   ";
    cin >> num2;
    cout << endl;

    cout << "Line 12: New value of num2 = "
         << num2 << endl;

    num3 = num1 + num2;

    cout << "Line 14: Num3 = " << num3 << endl;
    cout << "Line 15: " << num1 << " + " << num2
         << " = " << num1 + num2 << endl;
    cout << "Line 16: " << num1 << " * " << num2
         << " = " << num1 * num2 << endl;

    num3 = num1 - num2;

    cout << "Line 18: Num3 = " << num3 << endl;
    cout << "Line 19: " << num1 << " - " << num2
         << " = " << num1 - num2 << endl;
    cout << "Line 20: (" << num1 << ") / (" << num2
         << ") = " << num1 / num2 << endl;

    return 0;
}

fractionOverloading.cpp

#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

#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
MPNation
  • 179
  • 2
  • 4
  • 12
  • Please come up with an [MCVE](http://stackoverflow.com/help/mcve) rather than dumping your code. However, I can tell you that this error is [very clearly pointed out by Clang](http://coliru.stacked-crooked.com/a/3861b46e12f477ca), just in the middle of 500 other errors you have (and since the first ones aren't very clear, function definitions should not have semicolons before the body and should not have two return types). – chris Nov 05 '14 at 01:54
  • You may want to make your overloaded stream methods `public` instead of `private`. The default access for a `class` is `private`. – Thomas Matthews Nov 05 '14 at 02:01
  • 1
    @ThomasMatthews: The stream operators are not methods but are declared `friend`. They are unconditionally accessible but the `friend` declaration grants _them_ access the `private` part of `fractionType`. – Dietmar Kühl Nov 05 '14 at 02:03

2 Answers2

1

These are function declarations:

fractionType num2();
fractionType num3();

You probably meant:

fractionType num2;
fractionType num3;

which declare variables, constructed using the default constructor.

The last four error messages are from attempting to do arithmetic on a function pointer. If you see this sort of error message it can be a clue that you fell victim to "vexing parse", i.e. actually declared a function when you meant to declare a variable.

I think the first two error messages are a compiler bug? It should be an unambiguous conversion to bool. Perhaps the compiler has an extension to convert function pointer to void *.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • I have tried that and I got errors of undefined reference to 'frationType::fractionType()' but does not give a line number for the error. – MPNation Nov 05 '14 at 02:17
  • @KennyWatts That's a different error completely (you've moved on from your original error to a new error now). [See here](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400) for a full explanation; the short version is that you didn't actually tell your compiler to use all the files you just compiled. If you still have trouble then post a new question. – M.M Nov 05 '14 at 02:22
1

Your problem is a hideous issues known as Most Vexing Parse: your "objects" num2 and num2 are not objects but function declarations! When something can be considered a declaration rather than a statement, it is considered to be a declaration.

The viable fixes are

fractionType num1;                 // you should have a default constructor initializing members, though
fractionType num2{};               // this does the Right Thing but requires C++11
fractionType num3 = factionType(); // the Right Thing but requires a copy/move constructor
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • amongst other problems: `istream& operator >> (istream& isObject, const fractionType& myFraction)` probably shouldn't have a `const` param. – Mooing Duck Nov 05 '14 at 02:08