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
Used the GCC compiler to compile the exp. c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call.
The error: undefined reference to 'main' in C program is a very stupid mistake by the programmer, it occurs when the main() function does not exist in the program. If you used main() function and still the error is there, you must check the spelling of the main() function.
You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.
You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.
The declaration and definition of insertLike
are different
In your header file:
void insertLike(const char sentence[], const int lengthTo, const int length,
const char writeTo[]);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With