Reputation: 1137
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.
Upvotes: 1
Views: 110
Reputation: 230
There are a few mistakes in the code you posted.
First, the copy constructor is declared twice in the header file. Both these declarations are the same because their parameters are of the same type.
Fraction(const Fraction& fraction);
Fraction(const Fraction& orig);
Secondly, the constructors are defined twice: once in the header file, once in the source file. There can be only one definition. You can choose which one to keep. I usually prefer to define them in the source file (.cpp) in order to hide the implementation details.
Finally, if you keep the definition in the header file, a pair of brackets is missing. A valid implementation in the header file would look like this:
class Fraction {
private:
...
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()) {}
...
}
Upvotes: 2
Reputation: 32797
You have declared the copy constructor twice.
Fraction(const Fraction& fraction) ....
Fraction(const Fraction& orig);
Look at the signature. They are same, which can not be possible in C++.
You probably meant in the Fraction.h
Fraction(const Fraction &fraction); // only one
and in the Fraction.cpp
Fraction::Fraction(const Fraction& fraction)
: m_numerator(fraction.numerator())
, m_denominator(fraction.denominator())
{}
In short, you need to remove one of them.
Upvotes: 5