I have now figured out what was wrong. It was an issue with codeblocks simply not working properly (or perhaps some setting that I changed without being aware of). Starting a new project with default settings, and copy/pasting got me through it. The original problem is still written below in case anyone wants to read it.
I'm having some trouble building a C++ project I created using Codeblocks 13.12. When I try to build it, I'm given the error message
undefined reference to 'Prime_functions::isprime()'
I've seen this issue (or very similar ones mentioned here before) but the solutions I saw either confused me more than they helped, or just straight up did nothing.
Here's what the files in this project look like:
main.cpp
#include "Prime_functions.h"
#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
int main()
{
Prime_functions bo;
bo.isprime();
std::cout << bo.factor;
return 0;
}
Prime_functions.h
#ifndef PRIME_FUNCTIONS_H
#define PRIME_FUNCTIONS_H
class Prime_functions
{
public:
int isprime();
int primedetector();
int primelist();
int max_and_min_prime();
int factor;
protected:
private:
};
#endif // PRIME_FUNCTIONS_H
Prime_functions.cpp
#include "Prime_functions.h"
#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
int factor = 0;
int primedetector(int a){
int n = 2;
while(2*n <= a+1){
if(a%n == 0){
factor = n;
return a;
n = a+1;
}
if (a-1 < 2*n){
return 0;
}
n++;
}
}
int isprime(){
std::cout << "Enter a number and check if it's prime." << std::endl;
int a;
std::cin >> a;
std::cout << std::endl;
if(primedetector(a) == a){
std::cout << a << " is not a prime number. It is divisible by " << factor << std::endl;
}else if(primedetector(a) == 0){
std::cout << a << " is a prime number. " << std::endl;
}
std::cout << std::endl;
}
int primelist(){
int p,q;
std::cout << "Enter the lower limit for this search" << std::endl;
std::cin >> p;
std::cout << "Enter the upper limit for this search" << std::endl;
std::cin >> q;
std::cout << "----------" << std::endl;
if(p>q){
int q2 = q;
int p2 = p;
q = p2;
p = q2;
}
if(p<2){
p=2;
}
for(int z=p; z<q; z++){
if(primedetector(z)==0){
std::cout << z << std::endl;
}
}
}
int max_and_min_prime(){
int p,q;
std::cout << "Enter the lower limit for this search" << std::endl;
std::cin >> p;
std::cout << "Enter the upper limit for this search" << std::endl;
std::cin >> q;
std::cout << "----------" << std::endl;
if(p>q){
int q2 = q;
int p2 = p;
q = p2;
p = q2;
}
if(p<2){
p=2;
}
int current = 0;
for(int z=p; z<q; z++){
if(primedetector(z)==0){
if(current == 0){
current = z;
std::cout << "The prime smallest number in this range is " << current;
}
current = z;
}
}
std::cout << " and the largest is " << current << ".\n\n";
}
A couple of things that may or may not be significant:
- If I remove the line
bo.isprime();
frommain.cpp
it will compile, so the error might be related to that. Replacing it with one of the other functions seems to cause the same issue. - The line
std::cout << bo.factor;
was placed there to check if things were running properly. However, it prints out 1972859722 instead of the assigned value; 0. - The functions in the
Prime_functions.cpp
file worked without error as a standalone file (I have since deleted theint main()
function from it but made no other changes). - Adding return values to the
int functions
inPrime_finctions.cpp
does nothing. Making them void also does nothing.
That's pretty much all the relevant information I can think of right now.