简体   繁体   English

编译错误 C++ 未定义引用

[英]Compilation error C++ undefined reference

I'm a beginner and I'm currently feeling pretty lost about a compilation error that I'm getting since yesterday, so I was hoping someone could help me.我是一个初学者,我现在对从昨天开始遇到的编译错误感到非常迷茫,所以我希望有人能帮助我。 I'm writing the following program:我正在编写以下程序:

#include <iostream>
#include <algorithm>


void heapify(int);
void print(int);

const int MAX = 10;

int main () {
  int arr[MAX] = {2, 3, 4, 1, 12, 34, 11, 9, 22, 5};


  print(arr[MAX]);
  heapify(arr[MAX]);
  print(arr[MAX]);

  return 0;
}


void heapify(int arr[]) {
  int i = 0;
  int l = 2 * i;
  int r = 2 * i + 1;

for (i = 0; i < ::MAX; i++) {
  if (l <= i/2 && arr[l] > arr[i/2]) {
   std::swap(arr[l], arr[i/2]); 
    }
  if (r <= i/2 && arr[r] > arr[i/2]) {
    std::swap(arr[r], arr[i/2]);
    } 
  }
}

void print(int arr[]) {
  for (int i = 0; i < ::MAX; i++) {
    std::cout << arr[i] << " ";
  }
  std::cout << std::endl;
}

I'm then compiling it using the g++ -c main.cpp and g++ -o main main.cpp commands but I get the following compilation errors:然后我使用 g++ -c main.cpp 和 g++ -o main main.cpp 命令对其进行编译,但出现以下编译错误:

main.o:main.cpp:(.text+0x66): undefined reference to 'print(int)'
main.o:main.cpp:(.text+0x72): undefined reference to 'heapify(int)'
main.o:main.cpp:(.text+0x7e): undefined reference to 'print(int)'
collect2.exe: error: ld returned 1 exit status

I've looked up the type of error and I get that whenever I am getting a compilation error like undefined reference to main c++ or undefined reference to a c++ function then I have to add definition of that function inside my file. I've looked up the type of error and I get that whenever I am getting a compilation error like undefined reference to main c++ or undefined reference to a c++ function then I have to add definition of that function inside my file. I'm probably in the wrong but it seems to me that I have defined my functions correctly, though the program won't be executed successfully.我可能错了,但在我看来,我已经正确定义了我的函数,尽管程序不会成功执行。 I've consulted similar questions on StackOverflow about this error and, as far as I've seen, most of them have as a cause a missing.cpp file but I'm just compiling the main.cpp file so I don't really know what to do.我已经在 StackOverflow 上咨询过有关此错误的类似问题,据我所知,其中大多数都是因为缺少.cpp 文件,但我只是在编译 main.cpp 文件,所以我真的没有知道要做什么。

By any chance, can anyone point out what am I getting wrong?有任何机会,谁能指出我做错了什么?

Look at the two declaration of heapify .看一下heapify的两个声明。

The one before main : main之前的那个:

void heapify(int);

The one after main (which is also a definition): main 之后的那个(也是一个定义):

void heapify(int arr[]) { ...

It might not be totally obvious, but these two declarations declare two different functions, both named heapify but with different parameter types.这可能不是很明显,但是这两个声明声明了两个不同的函数,都命名为heapify但具有不同的参数类型。 One accepts an int and another accepts an int * (cleverly disguised as int [] -- in case of function parameters (but not elsewhere.) these two things are the same).一个接受一个int另一个接受一个int * (巧妙地伪装成int [] - 在 function 参数的情况下(但不是其他地方。这两件事是相同的)。 Both can happily coexist in a single program.两者都可以愉快地共存于一个程序中。 This is called "overloading", It is a powerful tool.这就是所谓的“重载”,它是一个强大的工具。 but you don't need it here.但你在这里不需要它。 You need one function named heapify , not two.您需要一个名为heapify的 function ,而不是两个。

The compiler complains about the first function, heapify(int) , which is never defined.编译器抱怨第一个 function heapify(int)从未定义过。

So fix the first declaration to have the same parameter types as the other one:因此,将第一个声明修复为与另一个声明具有相同的参数类型:

void heapify(int []);

The same goes for print . print也是如此。

Now if you try to compile your program, you will get compilation errors.现在如果你尝试编译你的程序,你会得到编译错误。 That's because the calls to heapify and print are wrong.那是因为对heapifyprint的调用是错误的。

When you declare an array, you put the size in the square brackets:声明数组时,将大小放在方括号中:

int arr[MAX];

When you use and array, you don't put the size in the square brackets.使用and 数组时,不要将大小放在方括号中。 To refer to a single element, you put its index in a square brackets:要引用单个元素,请将其索引放在方括号中:

int first = arr[0]; // for example.

To refer to the whole array, you don't use square brackets at all:要引用整个数组,您根本不使用方括号:

heapify(arr);

If you write arr[MAX] , the following happens.如果您编写arr[MAX] ,则会发生以下情况。

  1. The compiler treats MAX as just another index, no different from 0 or 1 or l/2 or anything else.编译器将MAX视为另一个索引,与01l/2或其他任何东西没有区别。
  2. It generates code to access the element with the index MAX , which does not exist .它生成代码以访问索引为MAX的元素,该索引不存在 Valid indices are 0 to MAX-1 .有效索引为0MAX-1 Accessing arr[MAX] is undefined behaviour .访问arr[MAX]未定义的行为 At run time the program would have crashed or produced unexpected results (or sometimes, by pure chance, expected results -- but only when you are looking at it, not when your professor is looking).在运行时,程序会崩溃或产生意想不到的结果(或者有时,纯粹是偶然的,预期的结果——但只有当你在看它的时候,而不是当你的教授在看的时候)。 So it's pretty bad.所以情况很糟糕。 However...然而...
  3. The type of arr[anything] is int , regardless of whether it exists or not. arr[anything]类型int ,无论它是否存在。 When you write heapify(arr[MAX]) , the compiler generates a call to heapify(int) .当您编写heapify(arr[MAX])时,编译器会生成对heapify(int)的调用。 But this function was never defined .但是这个 function 从未被定义 You defined heapify(int[]) which is a totally different function.您定义heapify(int[])是完全不同的 function。 And of course at the linking stage heapify(int) is not found.当然,在链接阶段heapify(int)没有找到。

Fix the calls to heapify and print and the program should compile.修复对heapifyprint的调用,程序应该可以编译。 (I have not tested it so I don't know if it works ). (我没有测试过,所以我不知道它是否有效)。

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

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