Compiler gives error about undefined reference to a function [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
Following is my code compiler says undefined reference to function . please elaborate what to do. Why does it give an error about undefined reference to the function isPalindrome() which is boolean?
int main()
{
cout << "Please enter a string: ";
cin >> input;
cout<<"Vowel Count"<<vowelcount(input);
while(1)
{
if(isPalindrome())
{
palindrome_count++;
}
}
cout<<"palindrome_count"<<palindrome_count;
}
bool isPalindrome(string input)
{
do{
if (input == string(input.rbegin(), input.rend())) {
cout << input << " is a palindrome\n";
}
}
while(1);
}

The error message is telling you exactly what you need to know.
IsPalindrome isn't defined in your code before you use it. Make sure to define it above where you reference it (i.e above main) or prototype the function.

I think, you have forgot function declaration. So, put the forword declaration above main() function. Like:
bool isPalindrome(string input);

Related

Symbol already defined [duplicate]

This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 2 years ago.
Having 2 simple files like that:
Main.c:
#include "Initialization.cpp"
int main() {
return 0;
}
and Initialization.cpp:
int main2() {
return 0;
}
I'm getting en error:
..."int __cdecl main2(void)" (?main2##YAHXZ) already defined in Initialization.obj...
What's peculiar when i complied the program the first time everything was ok. After recompilation this error starts appearing.
PS. I'm using Visual Studio c++ 2019
The preprocessor copies everything in the include file into Main.c which will look
int main2() {
return 0;
}
int main() {
return 0;
}
Both Initialization.o and Main.o now have definition for
main2(). Thus, you break the one definition rule and invoke undefined behavior.

Compiler error in introductory C++ example program [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm having some difficulty reproducing an example program of Object-Oriented Programming Using C++ described in "Encapsulation and Type Extensibility."
For simplicity's sake, I've cut out most of the code to focus on the specific error at hand:
#include <iostream> // Access standard IO library
#include <string> //Access type 'string'
using namespace std; //Use standard library namespace
const int max_length = 255;
class my_string {
public:
void assign(const char* st);
int length() const { return len; }
void print() const
{ cout << s << "\nLength: " << len << endl; }
private:
char s[max_length];
int len;
};
int main()
{
my_string one;
one.assign("I'm sorry Dave, I'm afraid I can't do that.");
one.print();
system("PAUSE");
}
When I try to compile, I get the error message:
[Linker error] undefined reference to 'my_string::assign(char const*)'
I'm not sure what I'm doing wrong. My best guess is that assign is incorrectly defined, since the main() block seems fine.
Edit:
The complete example as written in the book is:
In file string1.cpp
const int max_len = 255;
class my_string {
public:
void assign(const char* st);
int length() const { return len; }
void print() const
{ cout << s << "\nLength: " << len << endl; }
private:
char s[max_length];
int len;
};
int main()
{
my_string one, two;
char three[40] = {"My name is Charles Babbage."};
one.assign("My name is Alan Turing.");
two.assign(three);
cout << three;
cout << "\nLength: " << strlen(three) << endl;
if (one.length() <= two.length())
one.print();
else
two.print();
}
Linking and compiling errors are two different things. A compiler error means that you did something wrong in the syntax.
A linking error tells you that there is a part missing when the linker tries to put your program together.
[Linker error] undefined reference to 'my_string::assign(char const*)'
This error tells you that somewhere the promise was made to the compiler that my_string::assign(char const*) exists and can be used (by a declaration void assign(const char* st);). But in the linking step the linker cannot find that function.
If the error references a function that you have written, then you might have forgotten the definition of it or have mismatching signature between declaration and definition.
The compiler can't find it's definition.
Usually there is a header file (.h) where the class' declaration is put, including as less as possible and a source file (.cpp) that includes all the definitions.
The header file declarations tells the compiler which methods shall be available (as a promise),
the source file should contain the definition of the functions that are declared in the header file.
If they aren't defined, meaning there is no body for that function, it can't be executed. In your book, the code is both declared and defined, by writing the methods inside the class' definition.
You could do the same:
public:
void assign(const char* st) {
/* implementations of the assign method here
(or leave it empty for this example, but rather don't)*/
};
int length() const { return len; };
...

C++ Error: undefine reference to main in class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I'm using code block in linux. When I try to build my cpp file where class Position is define, it gives an error-
in function '_start':
undefined reference to 'main'
Even after object file has been created.
All this files are in the same folder
This is the cpp file where Position is define
#include "POSITION.h"
std::string Position::pos(int num) {
switch(num) {
case 0: return "\0";
break;
case 1: return "st";
break;
case 2: return "nd";
break;
case 3: return "rd";
break;
default: return "th";
break;
}
}
This is the header file
#pragma once
#include <iostream>
class Position
{
public:
std::string pos(int num);
};
Every C or C++ program starts at the main function, e.g.
int main(int argc, char **argv)
{
// do something
return 0;
}
Since your program has only a Position class and no main, the linker complains about that fact.
The issue is that all C++ programs need an entry point. The compiler is looking for int main(), but it does not exist, so the program can't be compiled linked. If you want you could just add something like int main() { return 0; } to the end of your program if all you're trying to do is make sure it builds properly.

undefined reference to `saving::rate' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 5 years ago.
I am write this code but when i compile this code with g++ in arch linux i recive this error
/tmp/ccG7axw1.o: In function `saving::calculate()':
saving.cpp:(.text+0x3a): undefined reference to `saving::rate'
/tmp/ccG7axw1.o: In function `saving::modify()':
saving.cpp:(.text+0x93): undefined reference to `saving::rate'
collect2: error: ld returned 1 exit status
saving.h
class saving{
private :
double savebal;
public :
saving(double newSavebal);
double calculate();
void modify();
static double rate;
};
saving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
saving :: saving(double newSavebal){
savebal = newSavebal;
}
double saving :: calculate(){
savebal += (savebal * (rate / 100))/12;
}
void saving :: modify(){
cout<<"Please enter the new rate"<<endl;
cin>>rate;
}
mainSaving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
void menu(saving );
int main(){
saving s1(500);
menu(s1);
}
void menu(saving s){
int m;
cout<<"1) calculate month interest\n";
cout<<"2) change rate of interest\n";
cin>>m;
switch(m){
case 1 :
s.calculate();
break;
case 2 :
s.modify();
break;
}
}
In Saving.h, you declared the static variable:
static double rate;
But you still need to define it (in other words instantiate it). To do so you should add this to Saving.cpp:
double saving::rate = 0;
Without that, the linker cannot find the actual variable, so any reference to it will result in a linker error.
Because saving is a static member, you have to have it initialized beforehand.
In your saving.cpp file, add this line after including all the headers:
double saving::rate = 0
Your code should then like this:
#include<iostream>
#include"saving.h"
using namespace std;
double saving::rate = 0; //With this line here
saving :: saving(double newSavebal){
savebal = newSavebal;
}

C++ header and class file - undefined reference [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
.h:
class ArithmeticCoding
{
public:
ArithmeticCoding();
static void test(QString text);
static QMap<QChar,int> letters_freq;
}
.cpp:
QMap<QChar, int> letters_freq;
ArithmeticCoding::ArithmeticCoding()
{
}
void ArithmeticCoding::test(QString text)
{
for(int i=0; i<text.length(); i++) letters_freq[text.at(i)]++;
}
I am getting
arithmeticcoding.cpp:-1: error: undefined reference to
`ArithmeticCoding::letters_freq'
Why?
Add this to exactly one of your CPPs
QMap<QChar,int> ArithmeticCoding::letters_freq;