简体   繁体   English

错误:C++ 中“未定义对‘函数’的引用”

[英]Error: "undefined reference to 'function'" in C++

I got an error while compiling C++:编译 C++ 时出现错误:

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

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?错误在哪里?

You have declared a global takeModulusLOOP function, then call it in main, without ever defining it.您已经声明了一个全局takeModulusLOOP 函数,然后在 main 中调用它,而无需定义它。 This is a different function than PowerModulus::takeModulusLOOP.这是与 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):改为命名空间而不是类,并分为头文件和实现(而不是在 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

This line:这一行:

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

is calling the non-class takeModulusLOOP , which you haven't defined anywhere.正在调用您尚未在任何地方定义的非类takeModulusLOOP

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.然后你不需要实例化一个。

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 类中拥有它,因此您应该从 PowerModulus 实例调用该函数。

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.您不需要在 .h 文件的开头或 .cpp 文件的开头声明该函数。

If you intended to use the takeModulusLoop function of the PowerModulus class then you need not declare a global function again...如果您打算使用PowerModulus类的takeModulusLoop函数,则无需再次声明全局函数...

But, if you intended to use a different global function, then you need to define it in its context...但是,如果您打算使用不同的全局函数,那么您需要在其上下文中定义它......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM