Introduction to C++ Undefined Reference
In C++ undefined reference is the kind of error which has occurred from when we have to create an object reference of any types like class, functions, variables, enumeration after creating that it will be used wherever it requires the linker will not find its definitions but it can be used to search the referenced objects including both files and libraries if the linker cannot find its definition of the created objects then the issue will be raised and it throws the error called undefined reference error this error will occur at different stages of the object creation and linking stages in the C++ codes.
Syntax:
In C++ each object, variables, keywords, and functions have their own syntax and attributes for declaring in the programming codes. Based on the requirements we will utilize the special keywords, variables, data types, and functions from the programming libraries. The undefined reference is one of the predefined errors from the functions and it is used to call whenever the format of the method linked not correctly to the object creation of the codes.
#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 above codes, we have function name () that function is declared using some arguments but in the main method, the function is called without arguments so when compiles the code it creates the undefined reference error.
How Undefined Reference works in C++?
The C++ having n number of reserved keywords, functions that will provide some level of abstractions from the actual namespaces as well as the new libraries which is being already used for the programmers to allow it with more focus on the coding concepts. It also makes it easier to write the programming codes and clean it 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 clean up the memory space areas. It depends upon the data types and the object creation sizes must be calculated and it allocates the memory space for both the big storage data type variables as well as small amount storage variables. Normally the C++ declarations and the directives are used with some kind of memory spaces allocated for the functions to store it in the RAM. The function declaration and the definition will be the standard type of protocols and it brings all the types of members and the functions are calculated into the current and future scopes.
Whenever we have used the method or function it will be called the main() method it must be used for creating the instance in the heap memory. The function will be valid it must be the argument or non-argument from the function the called function as the reference for to link the files and libraries the object linker sometimes can’t get the reference from the object definitions. These must be the various valid type of reasons should be included even though the coder also forget to define the object in the main method that’s the reason for receiving the undefined reference error during the compile time. The function declaration also sometimes wrongly defined from one way to another way because we used any type of object in the code but the definition is something different from what we actually called in the method. Files are also used more than one source files at the compile time but actually, the files are compiled separately so that time the objects are not linked properly so it also created undefined reference error.
Examples
Below are a few examples which demonstrate undefined reference.
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 logics will create the bug sometimes. But the programmer faces the error after written the codes during the compile time so it explores all the time durations including the segmentation fault, unresolved some 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 few examples respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses