C ++对定义函数的未定义引用 [英] C++ undefined reference to defined function

查看:192
本文介绍了C ++对定义函数的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这不起作用.我将放置所有三个文件,也许有人可以告诉我为什么它会引发此错误.我正在使用g ++来编译程序.

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.

程序:

#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

推荐答案

insertLike的声明和定义不同

在您的头文件中:

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, 字符writeTo [] );

C ++允许函数重载,在这里,您可以具有多个具有相同名称的函数/方法,只要它们具有不同的参数即可.参数类型是函数签名的一部分.

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.

在这种情况下,以const char*作为其第四参数的insertLike和以char *作为其第四参数的insertLike不同的功能.

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

这篇关于C ++对定义函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆