I am working on a practical assignment which requires us to use an external function to determine whether a integer entered is a prime number. I have created the header file, external functions file and main file but when i compile using
g++ -o main main.cpp extFunc.cpp
but I get the error:
/tmp/cca073oR.o: In function 'main':
main.cpp:(.text+0x42): undefined reference to 'isPrime(int)'
collect2: error: ld returned 1 exit status
The following are my cpp and header classes:
1)extFunc.h
bool isPrime(int num);
2)extFunc.cpp
# include "extFunc.h"
bool isPrime(int num) {
int i;
bool numPrime;
//Determine if number is prime
if (num <= 0) {
numPrime = false;
}
if (num = 1) {
numPrime = false;
}
if (num = 2) {
numPrime = true;
}
if (num = 3) {
numPrime = true;
}
else {
numPrime = true;
for(i = 2; i < num; i++) {
if ((num%i) == 0){
numPrime = false;
break;
}
}
}
//Return values
if (numPrime == true) {
return true;
} else {
return false;
}
}
3) main.cpp
#include <iostream>
#include "extFunc.h"
using namespace std;
int main() {
//Variables
int uNum;
bool prime;
cout << "Enter a number: ";
cin >> uNum;
prime = isPrime(uNum);
if (prime = true) {
cout << uNum << " is prime" << endl;
}
else {
cout << uNum << " is not prime" << endl;
}
return 0;
}
I have tried some of the other suggestions I was able to find on the site including using #ifndef in the header file but it did not fixed anything for me. I am unable to identify what possible could be the problem since the function in the header file and the function file is the same and it is called correctly (from what I can see) in the main file.