0

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(); from main.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 the int main() function from it but made no other changes).
  • Adding return values to the int functions in Prime_finctions.cpp does nothing. Making them void also does nothing.

That's pretty much all the relevant information I can think of right now.

Okinawa Sama
  • 55
  • 1
  • 1
  • 5
  • 3
    member functions must include a related scope in their out-of-line definition, so `int Prime_functions::primedetector(int a){ /**/ }` – Piotr Skotnicki Aug 13 '15 at 09:19
  • 1
    Also, you declare functions with a return type but no return statement in their body. – RamblingMad Aug 13 '15 at 09:22
  • I forgot to mention, that adding returns had absolutely no effect on my code running. When it was a standalone file, it ran without that. I actually already put returns into this code, but since they didn't affect its ability to run, I got rid of them. As for changing `primedetector(int a)` to `Prime_functions::primedetector(int a)` (and doing the same for other functions) that just gave me a different error message which is why I decided not to do that either. – Okinawa Sama Aug 13 '15 at 09:26
  • *" that just gave me a different error message"* what *other* error message? – Piotr Skotnicki Aug 13 '15 at 09:32
  • It's too long to fit into a single comment, [so here's a google doc of it](https://docs.google.com/document/d/1dIssRQAgT6m5wAEGb4TtHvRDmvr4lS9aWsYg0dWx_jw/edit?usp=sharing) – Okinawa Sama Aug 13 '15 at 09:40
  • @OkinawaSama well, because the declaration has an empty parameter list, while in the definition you declare parameter `(int a)`. unless the two match, the program won't compile – Piotr Skotnicki Aug 13 '15 at 09:50

0 Answers0