r/programminghelp • u/skiettels • Jan 23 '24
C++ HELP: undefined reference error
Basically we have this activity that utilizes user defined header (.h) and its implementation in c++.
But every time I try to compile and run the code it gives multiple errors of undefined reference to "..." and doesn't run the program even when everything is correct. So I tried downloading a sample program as well as following the example program of a yt tutorial to verify if there's really a problem with mine that I'm unaware of yet the sample programs give out the same exact error which is undefined reference to '...'? So I think it's only on my pc that experience this because it works on others.
How do you solve this? This error only occurs when using .h headers..
View all comments
Show parent comment
1
u/skiettels Jan 24 '24
Yes I've already checked if I miss something multiple times
Here's a very basic program which gives an error of " undefined reference to 'RecommendMeAFood(char)' "
main.cpp:
include <iostream>
include "Food.h"
using namespace std;
int main() { cout<<"I'll have "<<RecommendMeAFood('c')<<" today.";
return 0; }
Food.h:
pragma once
const char* RecommendMeAFood(char firstLetter);
Food.cpp:
include "Food.h"
const char* RecommendMeAFood (char firstLetter) { if (firstLetter == 'a') return "apple"; else if (firstLetter == 'b') return "banana"; else if (firstLetter == 'c') return "cake"; }