So I've been working on this program for a while, I'm finally finish but I have two error that I can not understand why.

My first error is when I am declaring the header f

//------------------- Mixed.cpp --------------               

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

ile>

it keep sending me an error message error C2143: syntax error : missing ';' before 'using'
I feel like this is a real simple mistake but I can not figure out what I am doing wrong? There isn't suppose to be a ; before using

my next error is about operator overloading

I wrote overloading function for +,-,/,* and I am getting the same error message for all of them. This is a view of my + function

int Mixed::GCD(int a, int b)
 {
     int tmp;
     while (b) 
     {
         tmp = b;
         b = a % b;
         a = tmp;
     }
     return a;
 }
Mixed operator+(const Mixed& f1, const Mixed& f2)
{
    int newnum, newnum2, gcd, wholeNumber;
    double remainder;
    
    Mixed r;

    newnum = (f1.integer * f1.denominator) + f1.numerator;


    newnum2 = (f2.integer * f2.denominator) + f2.numerator;

    r.numerator = (newnum * f2.denominator)+ (newnum2 * f1.denominator);

    // load result with the common denominator
    r.denominator = f1.denominator * f2.denominator;



    gcd = GCD(r.numerator, r.denominator);
     
    r.numerator /= gcd;
    r.denominator /= gcd;

    wholeNumber = r.numerator / r.denominator;
    remainder = (r.numerator % r.denominator);


    if (wholeNumber == 0)
        cout << remainder << "/" << r.denominator;
    else
        cout << wholeNumber << " " << remainder << "/" << r.denominator;
    return r;
}

My error code is : error C3861: 'GCD': identifier not found, I tried declaring the function GCD before the overload and after the overload function and still same error message

I am not sure if it is my header file that is having teh problem

#include <iostream>        // for ostream, istream
using namespace std;

class Mixed
{
    friend Mixed operator+(const Mixed& f1, const Mixed& f2);
    friend Mixed operator-(const Mixed& f1, const Mixed& f2);
    friend Mixed operator*(const Mixed& f1, const Mixed& f2);
    friend Mixed operator/(const Mixed& f1, const Mixed& f2);

    friend bool operator<(const Mixed& f1, const Mixed& f2);
    friend bool operator>(const Mixed& f1, const Mixed& f2);
    friend bool operator>=(const Mixed& f1, const Mixed& f2);
    friend bool operator<=(const Mixed& f1, const Mixed& f2);
    friend bool operator==(const Mixed& f1, const Mixed& f2);
    friend bool operator!=(const Mixed& f1, const Mixed& f2);

    friend istream& operator>>(istream& out, Mixed& f);
    friend ostream& operator<<(ostream& in, Mixed& f);


public:
    Mixed();                                // Set numerator = 0, denominator = 1.
    Mixed(int i, int n, int d);                // constructor with parameters
                                            // acts as conversion constructor

    void operator++();                        // ++ prefix
    void operator--();                        // -- prefix

    void operator++(int);                    // postfix ++
    void operator--(int);                    // postfix --

    double Evaluate();
    void ToFraction();
    void Simplify();

private:
    int integer, numerator, denominator;
    double result;
    int GCD(int, int);
}

Yes. The problem is in mixed.h

Just put a ';' after the class declaration and it will work.

#include <iostream>        // for ostream, istream
using namespace std;

class Mixed
{
    friend Mixed operator+(const Mixed& f1, const Mixed& f2);
    friend Mixed operator-(const Mixed& f1, const Mixed& f2);
    friend Mixed operator*(const Mixed& f1, const Mixed& f2);
    friend Mixed operator/(const Mixed& f1, const Mixed& f2);

    friend bool operator<(const Mixed& f1, const Mixed& f2);
    friend bool operator>(const Mixed& f1, const Mixed& f2);
    friend bool operator>=(const Mixed& f1, const Mixed& f2);
    friend bool operator<=(const Mixed& f1, const Mixed& f2);
    friend bool operator==(const Mixed& f1, const Mixed& f2);
    friend bool operator!=(const Mixed& f1, const Mixed& f2);

    friend istream& operator>>(istream& out, Mixed& f);
    friend ostream& operator<<(ostream& in, Mixed& f);


public:
    Mixed();                                // Set numerator = 0, denominator = 1.
    Mixed(int i, int n, int d);                // constructor with parameters
                                            // acts as conversion constructor

    void operator++();                        // ++ prefix
    void operator--();                        // -- prefix

    void operator++(int);                    // postfix ++
    void operator--(int);                    // postfix --

    double Evaluate();
    void ToFraction();
    void Simplify();

private:
    int integer, numerator, denominator;
    double result;
    int GCD(int, int);
}; There was no semi-colon here. Hence the error

First off, yes you need to add a semi-colon. BUT in addition you need to note that GCD is a member function of Mixed and you have extracted the operator to an external friend function [more on that later].

Since GCD does not actually touch the class, or use any of the variables of the class, the best way to fix this is to make it a static function. e.g.

class Mixed  
{
  // stuff here
   private:
     static int GCD(int,int);
};

Then change line 31 to this: gcd=Mixed::GCD(r.numerator,r.denominator); Just a questions : sorry, I have just seen a couple of people making stuff like this: friend Mixed operator*(const Mixed& f1, const Mixed& f2); .

Just why do you need the friend directive. Without it, this would work perfectly

class Mixed
{
   // other stuff
  public:

    Mixed operator*(const Mixed&,const Mixed&) const;
};

Mixed Mixed::operator*(const Mixed& A) const
{
   Mixed B;
   B.numerator=A.numerator*numerator;
   B.denominator=A.denominator*denominator;
   return B;
}

Mixed A;
Mixed B;
Mixed C = A*B;

The only time you might need the friend class is if you have a second type e.g. Mixed operator+(const double&,const Mixed&) const; and want to write Mixed A; Mixed C= 0.6+A; . Your operator<< is such a method. [Although the friend is removed by making operator<< external to the class and adding a write method to the class]

Sorry to add a long comment that was not asked for, but it seems a very strange way to set the class up and I could well have missed a trick here. I have seen this in several posts tonight and I am wondering why. [Feel free to ignore this part of the post completely if you wish].

Thank you all so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.