Getting c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: main.o:main.cpp:(.text+0x2b): undefined reference to (insert my methods in ComplexNumber.cpp) when compiling using a makefile after the G++ main.o ComplexNumber.o -o output is being ran.
I have tried checking online for places I may have missed a and used my other header files I have done in the past for reference to check for errors there, but I have had no luck. I am using cygdrive on windows to compile, but have also tried just using mingw on regular command prompt as well.
Header File: '''''''''''''
#include <stdio.h>
#ifndef COMPLEXNUMBER_H_
#define COMPLEXNUMBER_H_
template<typename T>
class ComplexNumber
{
public:
// a is a real number, b is a complex number
T a;
T b;
ComplexNumber();
ComplexNumber(T, T);
void toString();
ComplexNumber operator +(ComplexNumber<T>& c);
ComplexNumber operator *(ComplexNumber<T>& c);
bool operator ==(ComplexNumber<T>& c);
};
#endif /* COMPLEXNUMBER_H_ */
''''''''''''''
ComplexNumber.cpp ''''''''''''''
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "ComplexNumber.h"
using namespace std;
template <typename T>
ComplexNumber<T>::ComplexNumber()
{}
template <typename T>
ComplexNumber<T>::ComplexNumber(T a, T b)
{
this->a = a;
this->b = b;
}
template <typename T>
ComplexNumber<T> ComplexNumber<T>:: operator +(ComplexNumber& c)
{
ComplexNumber <T> temp;
// adds the complex numbers, (a+bi) + (c+di) = (a+b) + i(c+d)
temp.a = this->a + c.a;
temp.b = this->b + c.b;
return temp;
}
template <typename T>
ComplexNumber<T> ComplexNumber<T>:: operator *(ComplexNumber& c)
{
ComplexNumber<T> temp;
// multiplies complex numbers, (a+bi) + (c+di) = (a*c-b*d) + i(a*d + b*c)
temp.a = (this->a * c.a) - (temp.b * c.b);
temp.b = (temp.a * c.b) + (temp.b * c.a);
return temp;
}
template <typename T>
bool ComplexNumber<T>:: operator ==(ComplexNumber<T>& c)
{
ComplexNumber<T> temp;
// compares complex numbers to see if they're equal, (a+bi) == (c+di) -> if (a==c) & (b==d)
if (temp.a == c.a && temp.b == c.b)
{
cout<< "The complex numbers are equal."<<endl;
return true;
}
return false;
}
template <typename T>
void ComplexNumber<T>::toString()
{
cout<< "("<< this->a<< " + "<< this->b<< "i)"<<endl;
}
MakeFile: ''''''''''''''
all: ComplexNumber
ComplexNumber: main.o ComplexNumber.o
g++ main.o ComplexNumber.o -o output
main.o: main.cpp
g++ -c main.cpp
ComplexNumber.o: ComplexNumber.cpp
g++ -c ComplexNumber.cpp
clean:
rm *.o output
''''''''''''''