C++对已定义函数的未定义引用

发布于 2024-10-01 10:41:22 字数 1418 浏览 0 评论 0原文

我不明白为什么这不起作用。我将把我的所有三个文件放在一起,也许有人可以告诉我为什么它会抛出这个错误。我正在使用 g++ 来编译该程序。

程序:

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

using namespace std;

int main()
{
  char sentence[MAX_SENTENCE_LENGTH];
  char writeTo[] = "output.txt";
  int distanceTo,likePosition, length, numWords;
  cout << "ENTER A SENTENCE!   ";
  cin.getline(sentence, 299);
  length = strlen(sentence);
  numWords = wordCount(sentence, length);
  for(int x = 0; x < 3; ++x)
  {
    likePosition = likePos(numWords);
    distanceTo = lengthTo(sentence, likePosition, length);
    insertLike(sentence, distanceTo, length, writeTo);
  }
  return 0;  
}

函数文件:

void insertLike(const char sentence[],  const int lengthTo, const int length, char writeTo[])
{
  char part1[MAX_SENTENCE_LENGTH], part2[MAX_SENTENCE_LENGTH];
  char like[] = " like ";
  for(int y = 0; y < lengthTo; ++y)
    part1[y] = sentence[y];
  for(int z = lengthTo+1; z < length - lengthTo; ++z)
    part2[z] = sentence[z];
  strcat(part1, like);
  strcat(part1, part2);
  writeToFile(sentence, writeTo);
  return;
}

头文件:

void insertLike(const char sentence[], const int lengthTo, const int length, const char writeTo[]);

错误确切的是:

undefined reference to 'insertLike(char const*, int, int, char const*)'
collect2: ld returned 1 exit status

I cannot figure out why this is not working. I will put up all three of my files and possibly someone can tell me why it is throwing this error. I am using g++ to compile the program.

Program:

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

using namespace std;

int main()
{
  char sentence[MAX_SENTENCE_LENGTH];
  char writeTo[] = "output.txt";
  int distanceTo,likePosition, length, numWords;
  cout << "ENTER A SENTENCE!   ";
  cin.getline(sentence, 299);
  length = strlen(sentence);
  numWords = wordCount(sentence, length);
  for(int x = 0; x < 3; ++x)
  {
    likePosition = likePos(numWords);
    distanceTo = lengthTo(sentence, likePosition, length);
    insertLike(sentence, distanceTo, length, writeTo);
  }
  return 0;  
}

Function file:

void insertLike(const char sentence[],  const int lengthTo, const int length, char writeTo[])
{
  char part1[MAX_SENTENCE_LENGTH], part2[MAX_SENTENCE_LENGTH];
  char like[] = " like ";
  for(int y = 0; y < lengthTo; ++y)
    part1[y] = sentence[y];
  for(int z = lengthTo+1; z < length - lengthTo; ++z)
    part2[z] = sentence[z];
  strcat(part1, like);
  strcat(part1, part2);
  writeToFile(sentence, writeTo);
  return;
}

Header file:

void insertLike(const char sentence[], const int lengthTo, const int length, const char writeTo[]);

The error exactly is:

undefined reference to 'insertLike(char const*, int, int, char const*)'
collect2: ld returned 1 exit status

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

纵性 2024-10-08 10:41:22

insertLike 的声明和定义具有不同的 writeTo 参数。

在你的头文件中,你有:

void insertLike(const char sentence[], const int lengthTo, 
                const int length, **const char writeTo[]**);

在你的函数文件中:

void insertLike(const char sentence[],  const int lengthTo, 
                const int length, **char writeTo[]**);

C++允许函数重载,你可以有多个同名的函数/方法,只要它们有不同的参数。参数类型是函数签名的一部分。

在本例中,insertLike 采用 const char* 作为其第四个参数,insertLike 采用 char * 作为其第四个参数第四个参数是不同的功能

The declaration and definition of insertLike have different writeTo parameters.

In your header file, you have:

void insertLike(const char sentence[], const int lengthTo, 
                const int length, **const char writeTo[]**);

while in your function file:

void insertLike(const char sentence[],  const int lengthTo, 
                const int length, **char writeTo[]**);

C++ allows function overloading, where you can have multiple functions/methods with the same name, as long as they have different arguments. The argument types are part of the function's signature.

In this case, insertLike which takes const char* as its fourth parameter and insertLike which takes char * as its fourth parameter are different functions.

柳絮泡泡 2024-10-08 10:41:22

尽管以前的海报涵盖了您的特定错误,但如果您不告诉编译器使用 C 链接,则在尝试使用 g++ 编译 C 代码时,您可能会收到“未定义引用”链接器错误。

例如,您应该在 C 头文件中执行此操作:

extern "C" {

...

void myfunc(int param);

...

}

使“myfunc”在 C++ 程序中可用。

如果您仍然想在 C 中使用它,请将 extern "C" {} 包装在 #ifdef __cplusplus 预处理器条件中,如下

#ifdef __cplusplus
extern "C" {
#endif

所示这样,当使用 C 编译器时,extern 块将被“跳过”。

Though previous posters covered your particular error, you can get 'Undefined reference' linker errors when attempting to compile C code with g++, if you don't tell the compiler to use C linkage.

For example you should do this in your C header files:

extern "C" {

...

void myfunc(int param);

...

}

To make 'myfunc' available in C++ programs.

If you still also want to use this from C, wrap the extern "C" { and } in #ifdef __cplusplus preprocessor conditionals, like

#ifdef __cplusplus
extern "C" {
#endif

This way, the extern block will just be “skipped” when using a C compiler.

彡翼 2024-10-08 10:41:22

您需要编译所有源文件并将其链接在一起:

g++ main.c function_file.c

You need to compile and link all your source files together:

g++ main.c function_file.c
征﹌骨岁月お 2024-10-08 10:41:22

如果您使用 CMake,也可能会发生这种情况。如果您创建了一个新类并且想要实例化它,则在构造函数调用时您将收到此错误 - 即使标头和 cpp 文件正确 - 如果您尚未修改 CMakeLists.txt 相应地。

使用 CMake,每次创建新类时,在使用头文件之前,必须将 cpp 文件和任何其他可编译文件(如 Qt ui 文件)添加到 < code>CMakeLists.txt,然后重新运行存储 CMakeLists.txtcmake .

例如,在此 CMakeLists.txt 文件中:

cmake_minimum_required(VERSION 2.8.11)

project(yourProject)

file(GLOB ImageFeatureDetector_SRC *.h *.cpp)

### Add your new files here ###
add_executable(yourProject YourNewClass.h YourNewClass.cpp otherNewFile.ui})

target_link_libraries(imagefeaturedetector ${SomeLibs})

如果您使用命令 file(GLOB yourProject_SRC *.h *.cpp) 那么您只需重新运行 < code>cmake . 而不修改 CMakeLists.txt

This could also happen if you are using CMake. If you have created a new class and you want to instantiate it, at the constructor call you will receive this error -even when the header and the cpp files are correct- if you have not modified CMakeLists.txt accordingly.

With CMake, every time you create a new class, before using it the header, the cpp files and any other compilable files (like Qt ui files) must be added to CMakeLists.txt and then re-run cmake . where CMakeLists.txt is stored.

For example, in this CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.11)

project(yourProject)

file(GLOB ImageFeatureDetector_SRC *.h *.cpp)

### Add your new files here ###
add_executable(yourProject YourNewClass.h YourNewClass.cpp otherNewFile.ui})

target_link_libraries(imagefeaturedetector ${SomeLibs})

If you are using the command file(GLOB yourProject_SRC *.h *.cpp) then you just need to re-run cmake . without modifying CMakeLists.txt.

吲‖鸣 2024-10-08 10:41:22

如果您要包含的库依赖于另一个库,那么包含的顺序也很重要:

g++ -o MyApp MyMain.o -lMyLib1 -lMyLib2

在这种情况下,如果 MyLib1 依赖于 MyLib2 也没关系。
但是,如果情况相反,您将得到未定义的引用。

If you are including a library which depends on another library, then the order of inclusion is also important:

g++ -o MyApp MyMain.o -lMyLib1 -lMyLib2

In this case, it is okay if MyLib1 depends on MyLib2.
However, if there reverse is true, you will get undefined references.

ヅ她的身影、若隐若现 2024-10-08 10:41:22

正如 Paul 所说,这可能是链接器投诉,而不是编译器错误。如果您仔细阅读构建输出/日志(可能需要在单独的 IDE 窗口中查看才能查看完整的详细信息),如果问题来自编译器(需要在代码中修复)或来自链接器(并且需要在 make/cmake/project 级别进行修复以包含缺少的库)。

As Paul said, this can be a linker complaint, rather than a compiler error. If you read your build output/logs carefully (may need to look in a separate IDE window to see the full details) you can dell if the problem is from the compiler (needs to be fixed in code) or from the linker (and need to be fixed in the make/cmake/project level to include a missing lib).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文