Reputation: 421
I'm pretty sure this is probably something simple because I've followed what other people have already done on this website but I keep getting linking errors regardless of how I set up my files and #includes. Its quite odd, any who here is my code.
These three files will not compile with g++ main.cpp Fraction.cpp.
I get this error:
Undefined symbols for architecture x86_64:
"Fraction::simplify(int, int)", referenced from:
_main in w2-TsJ6zT.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is my main.cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
int main() {
Fraction fraction;
int num, den;
fraction.simplify(num, den);
}
This is my Fraction.h
class Fraction{
private:
//Variables
int numerator;
int denominator;
public:
//Methods
void simplify(int numerator, int denominator);
};
And finally my Fraction.cpp file.
#include <iostream>
using namespace std;
#include "Fraction.h"
void simplify(int numerator, int denominator){
//definition here
}
Any help and information is appreciated. Thanks!
Upvotes: 0
Views: 236
Reputation:
void simplify(int numerator, int denominator){
//definition here
}
should be
void Fraction::simplify(int numerator, int denominator){
//definition here
}
The way you defined it, simplify
is a simple function in global scope while the second variant implements the member function simplify
of Fraction
previously declared in Fraction.h
. The linking error tells you that you never implemented this method (because you actually implemented a new function with same name).
Upvotes: 3