Updated July 1, 2023
Introduction to C++ Undefined Reference
In C++, an “undefined reference” error occurs when an object reference, such as a class, function, variable, or enumeration, is created, but the linker cannot find its definition. This error arises when the object is referenced or used in the code, but the linker fails to locate the necessary definition. The linker searches for the referenced objects in both files and libraries. If the linker cannot find the definition of the created object, an “undefined reference” error is thrown.
Syntax:
In C++, each object, variables, keywords, and function have its own syntax and attributes for declaring in the programming codes. Based on the requirements, we will utilize the programming libraries’ special keywords, variables, data types, and functions.
#include<iostream>
using namespace std;
data type function name();
data type main()
{
function name();
--some c++ coding logics--
}
data type function name(parameters)
{
--some c++ coding logics—
}
In the code snippet you mentioned, there seems to be a mismatch in the function declaration and function call. To prevent an undefined reference error during compilation, programmers must call a function with the correct number and types of arguments if the function is declared with arguments.
How Undefined Reference works in C++?
In C++, there is a multitude of reserved keywords and functions that provide a certain level of abstraction, allowing programmers to focus more on coding concepts rather than dealing with low-level details, namespaces, and external libraries. It also makes it easier to write the programming codes and clean them up using the same methods like destroying () or any other default methods belonging to the garbage collections, and it’s the main area for destroying the unwanted codes and cleaning up the memory space areas.
Whenever we have used the method or function, it will be called the main() method. The function will be valid. The object linker sometimes can’t get the reference from the object definitions.
Examples
Below are a few examples that demonstrate undefined references.
Example #1
Code:
#include <iostream>
using namespace std;
struct demo {
virtual void one(int) { std::cout << "demo::one\n"; }
void two(char) { std::cout << "demo::two\n"; }
void three(int) { std::cout << "demo::three\n"; }
protected:
int a;
typedef int val;
};
struct demo1 : demo {
using demo::a;
using demo::val;
using demo::one;
void one(int) { std::cout << "demo1::one\n"; }
using demo::two;
void two(int) { std::cout << "demo1::two\n"; }
using demo::three;
void three(int) { std::cout << "demo1::three\n"; }
};
int example();
int main()
{
demo1 i;
demo& d = i;
int m;
i.a = 3;
i.one(3);
i.one(3);
i.two(2);
i.two('k');
i.three(3);
i.three(3);
cout<<"Please enter your number:";
cin>>m;
if(m>=0)
cout<<"Welcome User the absolute value of your input is:"<<m;
else
cout<<"Thank you User your absolute value of the input is"<<-(m);
example();
return 0;
}
Output:
Example #2
Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool demo(char a)
{
return (a >= '0' && a <= '9')
? true
: false;
}
int demo1(char* s)
{
if (*s == '\0')
return 0;
int b = 0;
int s1 = 1;
int p = 0;
if (s[0] == '-') {
s1 = -1;
p++;
}
for (; s[p] != '\0'; ++p) {
if (demo(s[p]) == false)
return 0;
b = b * 10 + s[p] - '0';
}
return s1 * b;
}
int example();
int example(int eg)
{
cout<<"Welcome To My Domain";
}
int main()
{
char c[] = "-987654";
int output = demo1(c);
printf("%d ", output);
int m = -3;
long n = -87645;
int l = abs(m);
long w = abs(n);
cout << "abs(" << m << ") = |" << m << "| = " << l << endl;
cout << "abs(" << n << ") = |" << n << "| = " << w << endl;
example();
return 0;
}
Output:
Example #3
Code:
#include <iostream>
#include <string>
using namespace std;
extern int a;
extern void demo();
void demo1() {
++a;
demo();
}
int main() {
cout<<"Welcome To My Domain";
demo1();
demo();
}
Output:
Conclusion
Generally, the programming codes are written with some logic that logic will create the bug sometimes. But the programmer faces the error after writing the codes during the compile time so it explores all the time durations, including the segmentation fault, unresolved external operators or symbols, and also the undefined reference errors in the code at any time.
Recommended Articles
This is a guide to C++ Undefined Reference. Here we discuss the introduction and How Undefined Reference works in C++? along with a few examples respectively. You may also have a look at the following articles to learn more –