jeudi 30 juillet 2020

Why am I getting a re-definition error in C++?

I wrote a Fraction class to do some work with Fraction objects and overloading and such and I need to separate the implementation from the definitions of the methods but I get a redefinition error when it comes to the constructors of the class Fraction.

Code snippet from Fraction.h

class Fraction
{
private:
   int calcGCD(int n1, int n2) const;
   int compare(const Fraction& fraction) const;
   int m_numerator;
   int m_denominator;
public:
   Fraction(int numerator = 0, int denominator = 1) : m_numerator(numerator), m_denominator(denominator);
   Fraction(const Fraction& fraction) : m_numerator(fraction.numerator()), m_denominator(fraction.denominator());
   Fraction(const Fraction& orig);
};

Code snippet from Fraction.cpp

#include "Fraction.h"

Fraction::Fraction(int numerator, int denominator) 
   : m_numerator(numerator), m_denominator(denominator)
{}

Fraction::Fraction(const Fraction& fraction)
   : m_numerator(fraction.numerator()), m_denominator(fraction.denominator())
{}

This is resulting in the following errors:

Fraction.h:26:5: error: 'Fraction::Fraction(const Fraction&)' cannot be overloaded with 'Fraction::Fraction(const Fraction&)'
Fraction.h:25:5: note: previous declaration 'Fraction::Fraction(const Fraction&)'
Fraction.h:24:105: error: expected '{' at end of input

Followed by some others which I think are just a cascading effect of one or two main errors. But I can post them if really needed.

I think it has something to do with how I'm declaring the constructor in the .cpp file because I know some things do not carry over such as access modifiers and static and etc.

Sorry if this is a silly error, I am new to C++ and I can not find the answer anywhere.

Aucun commentaire:

Enregistrer un commentaire