'Error: "undefined reference to 'function'" in C++

I got an error while compiling C++:

/tmp/ccqs6UN2.o: In function main': PowerModulus.cpp:(.text+0x194): undefined reference to takeModulusLOOP(int, int, int)' collect2: ld returned 1 exit status

The source code:

#include "PowerModulus.h"
#include <iostream>

int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);

int main() {
    std::cout << takeModulusLOOP(5348, 700, 335);
}

int PowerModulus::takeModulusLOOP(int x, int n, int moduint) {
    int total = modint(x, moduint);
    n--;
    while (--n) {
        total = modint(total * x, moduint);
    }
    return total;
}

int PowerModulus::modint(int x, int moduint) {
    while (x < 0) // Deal with negative
        x += moduint;
    return x % moduint; // Comes out positive now -> %
}

PowerModulus::PowerModulus() {
    // TODO Auto-generated constructor stub

}

PowerModulus::~PowerModulus() {
    // TODO Auto-generated destructor stub
}

Header file:

#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_

int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);

class PowerModulus {
    public:
        int takeModulusLOOP(int x, int n, int moduint);
        int modint(int x, int moduint);
        PowerModulus();
        virtual ~PowerModulus();
};

#endif /* POWERMODULUS_H_ */

Where is the error?

c++


Solution 1:[1]

You have declared a global takeModulusLOOP function, then call it in main, without ever defining it. This is a different function than PowerModulus::takeModulusLOOP.

// main.cpp
#include "PowerModulus.h"
#include <iostream>

int main(){
    std::cout << PowerModulus::takeModulusLOOP(5348,700,335) << '\n';
    return 0;
}

Changed to a namespace instead of a class, and separated into header and implementation (instead of grouping in main.cpp):

// PowerModulus.cpp
#include "PowerModulus.h"

namespace PowerModulus {

int takeModulusLOOP(int x, int n, int moduint){
    int total = modint(x, moduint) ;
    n--;
    while (--n){
        total = modint( total * x, moduint );
    }
    return total;
}

int modint(int x, int moduint){
    while ( x < 0) // deal with negative
        x += moduint;
    return x % moduint;//comes out positive now -> %
}

}

Header:

// PowerModulus.h
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_

namespace PowerModulus {

int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);

}

#endif

Solution 2:[2]

This line:

std::cout << takeModulusLOOP(5348,700,335);

is calling the non-class takeModulusLOOP, which you haven't defined anywhere.

You should either call the class version, by providing an object of the class type and using something like:

PowerModulus p;
std::cout << p.takeModulusLOOP(5348,700,335);

(most likely) or providing a non-class version (least likely).

You could also consider making the function static since it doesn't seem to require an object at all. Then you don't need to instantiate one.

Solution 3:[3]

You receive the error, because you do not have such a function.

Actually, you have it in PowerModulus class, so you should call the function from PowerModulus instance.

PowerModulus pM;
pM.takeModulusLoop(5348,700,335);

You do not need to claim the function in the beginning of your .h file or in the beginning of your .cpp file.

Solution 4:[4]

If you intended to use the takeModulusLoop function of the PowerModulus class then you need not declare a global function again...

But, if you intended to use a different global function, then you need to define it in its context...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Edleson
Solution 2 paxdiablo
Solution 3 Peter Mortensen
Solution 4 Peter Mortensen