C++: Errors while compilation of C++ program - c++

I am making a class Fraction with global functions usage
My code looks like below:
#include<iostream>
using namespace std;
ostream & operator<<(ostream & os, Fraction & fr)
{
return os << fr.get_num() << '/' << fr.get_den();
}
class Fraction
{
private:
int num, den;
public:
int get_num()
{
return num;
}
int get_den()
{
return den;
}
};
Main function has call : `cout << f2 << endl;
But i am getting following build erros while compilation:
Error C2805 binary 'operator <<' has too few parameters
fr: undeclared identifier
left of get_num must be struct/union/class

You should change the order of your code like this:
class Fraction
{
private:
int num, den;
public:
int get_num()
{
return num;
}
int get_den()
{
return den;
}
};
ostream & operator<<(ostream & os, Fraction & fr)
{
return os << fr.get_num() << '/' << fr.get_den();
}

Related

where's the error and why this program is wrong?

I'm doing an exercise for homework where i have to create a class for a fraction , but when i was coding i got in trouble with the overloading of the operator << ,and i don't understand where's the error because when the program reads the input the two numbers(numerator,denominator) are saved correctly, but when i try to print in output the fraction it gives me a random number; why?
#include <iostream>
using namespace std;
class Frazione{
private:
int num;
int den;
public:
Frazione operator+(Frazione f2){
Frazione risultato;
risultato.den=MCM(f2.den);
risultato.num=(num*(risultato.den/den))+(f2.num*(risultato.den/f2.den));
return risultato;
}
int MCM(int a){
int min;
int k;
if(den>a){
min=a;
}
else{
min=den;
}
for(int i=min;!(den%i==0 && a%min==0);i+=min){
k=i;
}
return k;
}
void setn(int a){
num=a;
}
void setd(int a){
den=a;
}
int getn(){
return num;
}
int getd(){
return den;
}
};
istream& operator>>(istream& in,Frazione f){
int n,d;
cout<<"Dimmi il numeratore"<<endl;
in>>n;
cout<<"Dimmi il denominatore"<<endl;
in>>d;
f.setn(n);
f.setd(d);
return in;
}
ostream& operator<<(ostream& out,Frazione& f){
out<<f.getn();
out<<"/";
out<<f.getd();
return out;
}
int main(){
Frazione f1,f2,f3;
cin>>f1;
cout<<f1;
cin.get();
return 0;
}
Currently don't pay attention to funciont MCM and operator+ because they are function that i have to implement then i'll solve this plobem.
You have missed the reference of the second parameter here:
istream& operator>>(istream& in,Frazione &f)
The parameter f is only accessible by reference.

It says "No matching constructor for initialization of 'Fraction'

.cpp
//
// calculator.cpp
//
#include "Fraction.h"
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
char op;
try
{
cin >> x;
cin >> op;
while ( cin && ( op == '+' || op == '-' ) )
{
cin >> y;
if ( op == '+' )
x = x + y;
else
x = x - y;
cin >> op;
}
cout << x << endl;
}
catch ( invalid_argument& e )
{
cout << "Error: " << e.what() << endl;
}
}
.h
#ifndef Fraction_Calculator_Fraction_h
#define Fraction_Calculator_Fraction_h
#include<iostream>
#include<cstdlib>
//Fraction class definition
class Fraction
{
public:
Fraction (int a, int b);
int fraction(int a, int b);
void set(int, int);
int get_numerator(void);
int get_denomenator(void);
int find_gcd (int n1, int n2);
void reduce_fraction(int nump, int denomp);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend std::ostream& operator<<(std::ostream &os, const Fraction& n);
friend std::istream& operator>>(std::istream &is, Fraction& n);
Fraction& operator= (const Fraction& n);
int denom;
int numera;
private:
int numerator;
int denomenator;
int denomp;
int nump;
};
#endif
It says "No matching constructor for initialization of 'Fraction' on the first line of the cpp file
I don't understand what it means.
The problem is that your Fraction constructor takes 2 arguments.
Fraction (int a, int b);
and you are invoking it with none
Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
You should either invoke x and y with the 2 int parameters or define another constructor that takes no arguments.
Provide default constructor like
Fraction()
{
numerator=0;
denomenator0;
denomp0;
nump=0;
}

Getting unresolved external error when operator overloading, even after defining all the methods

EDIT: need to delete post -- problem was trivial (typographical error) and won't be of any help to others
I'm getting errorLNK2019, the unresolved external error, when trying to use an operator overload in one of my cpp files. I've looked all over, and many people have been able to fix the problem by making sure to define every single method in their class prototypes.
I think this has to do a lot with my project design to be honest, but I really can't pinpoint exactly why this error is happening
Here's the code:
//a.cpp
//
// ... skipped all code to bottom to where i modified it
//OVERLOADED FUNCTIONS
int operator+(const int n, const a& entry){
return n + entry.getTime();
}
ostream& operator<<(ostream & out, const a& entry){
out << entry.getTitle() << " by " << entry.getArtist()
<< " (" << entry.getTime() << ") ";
return out;
}
//*********************************************************
// a.h
//... only posting what I changed
//
// Inside the class..
class a
{
public:
friend ostream& operator<<(const ostream& out, const a& entry);
friend int operator+(const int n, const a& entry);
//..
//.. SNIPPED
//..
}
I run into the error when I try to output a b object in the show() method.
//b.cpp
#include "b.h"
b b::etnsl(const int &indexOfItemToAdd) const{
if (originalObjects != NULL && indexOfItemToAdd >= (*originalObjects).size()){
throw "Index out of bounds";
}
b x(originalObjects);
vector<int> *iCopy = x.getIndices();
(*iCopy) = indices;
iCopy->push_back(indexOfItemToAdd);
x.setSum(sum + (*originalObjects)[indexOfItemToAdd].getTime());
return x;
}
void b::show() const{
cout << " Item at loc " << "0x" << this << ":" << endl;
//int j = indices.size();
//if (j == 0)
if (size == 0)
cout << " Empty item." << endl;
else{
for (int i = 0; i < size; i++) //ERROR IN LOOP
cout << " Index " << indices[i] << " : " << (*originalObjects)[indices[i]] << endl;
}
}
int b::getSum() const{
return sum;
}
void b::setSum(const int& num){
sum = num;
}
vector<int>* b::getIndices(){
return &indices;
}
//*********************************************************
//b header class
#ifndef B_H
#define B_H
#include <iostream>
#include <vector>
#include "a.h"
using namespace std;
class b{
private:
int sum, size;
vector <a> *originalObjects;
vector <int> indices;
public:
b(vector<a> *orig = NULL) //counts as 2 constructors: default and a custom one.
: sum(0), originalObjects(orig), size(indices.size()) {
}
b etnsl(const int &indexOfItemToAdd) const;
void show() const;
int getSum() const;
void setSum(const int& num);
vector<int> *getIndices();
};
#endif
ostream& operator<<(ostream & out, const iTunesEntry& tune)
is not same as
ostream& operator<<(const ostream& out, const iTunesEntry& entry);
// ~~~~~
inside the iTunesEntry class (friend method)
And const shouldn't be used as you will have to modify the out object of ostream

error: overloaded 'operator<<' must be a binary operator (has 3 parameters)

I know there are plenty of questions like these, but I couldn't find a solution that worked for me.
I am trying to make simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction.
Example: input=
3/2 + 4/
8
, output =
2
I am trying overload operators in order to accomplish this.
So in the program I am trying to develop, the input consists of an expression made of fractions separated by the operators + or -.
The number of fractions in the expression is arbitrary.
Each of the following 6 lines is an example of a valid input expression:
1/2 + 3/4
1/2 -5/7+3/5
355/113
3 /9-21/ -7
4/7-5/-8
-2/-3+7/5
***The problem that I am having is that in when I run my program it has an overload operating error: *error: overloaded 'operator<<' must be a binary operator (has 3 parameters)****
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:61:22: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
ostream& Fraction::operator<<(ostream &os, Fraction& n)
^
/Users/Spicycurryman/Desktop/ECS40/hw1/fraction.cpp:80:22: error: overloaded 'operator>>' must be a binary operator (has 3 parameters)
istream& Fraction::operator>>(istream &os, Fraction& n)
I don't understand why that is an error.
My following code is below:
CPP FILE
#include "Fraction.h"
Fraction::Fraction(int a, int b)
{
}
int Fraction::find_gcd (int n1, int n2)
{
int gcd, remainder;
remainder = n1 % n2;
while ( remainder != 0 )
{
n1 = n2;
n2 = remainder;
remainder = n1 % n2;
}
gcd = n2;
return (gcd);
}
void Fraction::reduce_fraction(int nump, int denomp)
{
this->nump = nump;
this->denomp = denomp;
int gcd;
gcd = find_gcd(nump, denomp);
nump = nump / gcd;
denomp = denomp / gcd;
if ((denomp<0 && nump < 0 ))
{
denomp*=-1;
nump*=-1;
}
else if (denomp < 0 && nump >0){
denomp*=-1;
}
if ( denomp ==0) {
throw invalid_argument( "Error: zero denominator" );
}
}
Fraction& Fraction::operator+(const Fraction& n) {
denom = denomp * n.denom;
numera = (nump * n.numera) + (n.denom * n.nump);
return (*this);
}
Fraction& Fraction::operator-(const Fraction& n) {
denom = denomp * n.denom;
numera = (nump * n.numera) - (n.denom* n.nump);
return (*this);
}
ostream& Fraction::operator<<(ostream &os, Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os;
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
istream& Fraction::operator>>(istream &os, Fraction& n)
{
char slash = 0;
return os >> n.numera >> slash >> n.denom;
}
Header File
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <stdexcept>
using namespace std;
class Fraction{
public:
Fraction(int a, int b);
int fraction(int a,int b);
int find_gcd(int n1, int n2);
void reduce_fraction(int nump, int denomp);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
private:
int denom;
int numera;
int denomp;
int nump;
};
#endif
MAIN CPP FILE
#include "Fraction.h"
#include <iostream>
using namespace std;
int main()
{
Fraction x(2,3);
Fraction y(6,-2);
cout << x << endl;
cout << y << endl;
cin >> y;
cout << y << endl;
Fraction z = x + y;
cout << x << " + " << y << " = " << z << endl;
}
I know that the operators are member functions and a member function takes an implicit first parameter, meaning my operators now takes three parameters it may be fixed being a non-member function; however, that would not work in this program. How exactly in my case would I fix it so the program would work?
Thank you very much!
The problem is that you declared operator>> and operator<< as non-member functions, but defined as a member function.
This should fix that problem (but open another set of problems). So instead of
ostream& Fraction::operator<<(ostream &os, Fraction& n)
{
...
istream& Fraction::operator>>(istream &os, Fraction& n)
{
...
implement as :
ostream& operator<<(ostream &os, Fraction& n)
{
...
istream& operator>>(istream &os, Fraction& n)
{
...
Also, take a note that you declared functions as :
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
but defined as (therefore you changed the signature) :
ostream& Fraction::operator<<(ostream &os, Fraction& n)
istream& Fraction::operator>>(istream &os, Fraction& n)
Proper way is to declare and define as :
ostream& Fraction::operator<<(ostream &os, const Fraction& n)
istream& Fraction::operator>>(istream &os, Fraction& n)
I am adding just changes. The rest is the same as in the question:
class Fraction{
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, Fraction& n);
// the rest is the same
};
ostream& operator<<(ostream &os, const Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os;
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
istream& operator>>(istream &os, Fraction& n)
{
char slash = 0;
return os >> n.numera >> slash >> n.denom;
}

Redefinition of class in CPP File Error

I know there are plenty of questions like these, but I couldn't find a solution that worked for me.
Okay, I am trying to make simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction.
Example: input=
3/2 + 4/
8
, output =
2
I am trying overload operators in order to accomplish this.
So in the program, I am trying to develop the input consists of an expression made of fractions separated by the operators '+'or '-'.
The number of fractions in the expression is arbitrary.
Each of the following 6 lines is an example of valid input expression:
1/2 + 3/4
1/2 -5/7+3/5
355/113
3 /9-21/ -7
4/7-5/-8
-2/-3+7/5
The problem that I am having is that in when I run my Main CPP program it has a class redefinition error:
fraction.cpp:6:7: error: redefinition of 'Fraction'
class Fraction
^
./Fraction.h:7:7: note: previous definition is here
class Fraction{
However, that should not be a problem because I declared the class and member functions in my header files and defined them in my CPP file.
My following code is below:
Header File
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction{
public:
Fraction(int , int );
int fraction(int,int);
void reduce_fraction(int *, int *);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
};
#endif
CPP File
#include <iostream>
using namespace std;
#include "Fraction.h"
#include <stdexcept>
class Fraction
{
public:
Fraction::Fraction(int a, int b);
int find_gcd(int n1, int n2);
void reduce_fraction(int *nump, int *denomp)
{
int gcd;
gcd = find_gcd(*nump, *denomp);
*nump = *nump / gcd;
*denomp = *denomp / gcd;
if ((*denomp<0 && *nump < 0 ))
{
*denomp*=-1;
*nump*=-1;
}
else if (*denomp < 0 && *nump >0){
*denomp*=-1;
}
if ( *denomp ==0) {
throw invalid_argument( "Error: zero denominator" );
}
}
Fraction& Fraction::operator+(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) + (n.denom * n.nump);
return Fraction(numera,denom);
}
Fraction& Fraction::operator-(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) - (n.denom* n.nump);
return Fraction(numera, denom);
}
friend ostream& operator<<(ostream &os, Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
friend istream& operator>>(istream &os, Fraction& n)
{
char slash = 0;
return is >> n.numera >> slash >> n.denom;
}
};
#include <iostream>
using namespace std;
#include "Fraction.h"
#include <stdexcept>
class Fraction
{
public:
Fraction::Fraction(int a, int b);
int find_gcd(int n1, int n2);
void reduce_fraction(int *nump, int *denomp)
{
int gcd;
gcd = find_gcd(*nump, *denomp);
*nump = *nump / gcd;
*denomp = *denomp / gcd;
if ((*denomp<0 && *nump < 0 ))
{
*denomp*=-1;
*nump*=-1;
}
else if (*denomp < 0 && *nump >0){
*denomp*=-1;
}
if ( *denomp ==0) {
throw invalid_argument( "Error: zero denominator" );
}
}
Fraction& Fraction::operator+(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) + (n.denom * n.nump);
return Fraction(numera,denom);
}
Fraction& Fraction::operator-(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) - (n.denom* n.nump);
return Fraction(numera, denom);
}
friend ostream& operator<<(ostream &os, Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
friend istream& operator>>(istream &os, Fraction& n)
{
char slash = 0;
return is >> n.numera >> slash >> n.denom;
}
};
MAIN CPP FILE
#include "Fraction.h"
#include <iostream>
using namespace std;
int main()
{
Fraction x(2,3);
Fraction y(6,-2);
cout << x << endl;
cout << y << endl;
cin >> y;
cout << y << endl;
Fraction z = x + y;
cout << x << " + " << y << " = " << z << endl;
}
I am essentially having trouble with understanding why I am getting the previous definition error and how exactly to fix it. Also, if you see anything else wrong with my implementation in this program I would appreciate you telling me!
Thank you very much!
The way to define member functions in Fraction.cpp is not like this:
class Fraction
{
public:
void reduce_fraction(int *nump, int *denomp)
{
...
}
}
but rather like this:
void Fraction::reduce_fraction(int *nump, int *denomp)
{
...
}
More generally, you should not write this much code before testing any of it.