1

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?

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

I don't know why in this situation I do not need to define :
#include "ex1.h" #include <iostream> void print(); int main(){ print(); return 0; } void print(){ std::cout << "Hello" << std::endl; } ex1::ex1() { // TODO Auto-generated constructor stub } ex1::~ex1() { // TODO Auto-generated destructor stub }
#ifndef EX1_H_ #define EX1_H_ void print(); class ex1 { public: void print(); ex1(); virtual ~ex1(); }; #endif /* EX1_H_ */
@silentbang, in those last three comments, you have a global print (straight after main and nothing to do with the ex1 class). Not so in your original code in the question, where takeModulosLOOP does belong to the class.
2

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.

1 Comment

I don't know why, in this case ommiting claiming prototype in the beginning of .cpp and .h is ok, but all the time it does not
0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.