what-are-undefined-reference/unresolved-external-symbol-errors-in-c++?

Linker discrepancies, such as “undefined reference” or “unresolved external symbol,” can pose challenges for C++ programmers trying to progress. This sort of error particularly arises when the code appears to compile correctly but fails during linking. It may also happen if the linker is unable to locate the definition of a declared function, variable, or object. Common reasons include missing function definitions, incorrect signatures, absent object files, or libraries that have not been linked. In the following article, we will examine these errors, their frequent causes, techniques for effective resolution, and several illustrative examples.

Contents:

Linker Discrepancies in C++

Linker discrepancies such as “undefined reference” or “unresolved external symbol” arise notably when the code appears to be compiled and the linking process encounters issues. These errors can also occur if the linker fails to ascertain the definition of a declared function, variable, or object. Frequent causes include missing function definitions, inappropriate signatures, absence of object files, or libraries not being linked.

1. What is an Undefined Reference Error in C++?

An undefined reference error is triggered whenever the linker is unable to locate the definition of a function or variable declared and used in the program. Thus, contrary to a compilation error, which halts the compilation process, an undefined reference suggests that the linking stage could not identify an implementation of an entity recognized by the compiler in the source file.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth “);

editor88877.setValue(decodedContent); // Set the default text editor88877.clearSelection();

editor88877.setOptions({ maxLines: Infinity });

function decodeHTML88877(input) { var doc = new DOMParser().parseFromString(input, “text/html”); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard88877() { const code = editor88877.getValue(); // Get code from the editor navigator.clipboard.writeText(code).then(() => { jQuery(“.maineditor88877 .copymessage”).show(); setTimeout(function() { jQuery(“.maineditor88877 .copymessage”).hide(); }, 2000); }).catch(err => { console.error(“Error copying code: “, err); }); }

function runCode88877() { var code = editor88877.getSession().getValue();

jQuery(“#runBtn88877 i.run-code”).show(); jQuery(“.output-tab”).click();

jQuery.ajax({ url: “https://intellipaat.com/blog/wp-admin/admin-ajax.php”, type: “post”, data: { language: “cpp”, code: code, cmd_line_args: “”, variablenames: “”, action:”compilerajax” }, success: function(response) { var myArray = response.split(“~”); var data = myArray[1];

jQuery(“.output88877”).html(“

"+data+"");
        jQuery(".maineditor88877 .code-editor-output").show();
        jQuery("#runBtn88877 i.run-code").hide();
    }
})
}						
```javascript
closeoutput88877() {
var code = editor88877.getSession().getValue();
jQuery(".maineditor88877 .code-editor-output").hide();
}

// Bind event listeners to the buttons
document.getElementById("copyBtn88877").addEventListener("click", copyCodeToClipboard88877);
document.getElementById("runBtn88877").addEventListener("click", runCode88877);
document.getElementById("closeoutputBtn88877").addEventListener("click", closeoutput88877);

Output:

undefined reference error

Explanation: The code illustrates how a function, myFunction(), is declared but lacks a definition, causing an undefined reference error during the linking step since the linker fails to locate its implementation.

2. What does an Unresolved External Symbol Error mean in C++?

An unresolved external symbol error is akin to an MSVC undefined reference error. It arises whenever an object file or library is absent during the linking process, leading to missing function or variable definitions.

Example:

Cpp

Code Duplicated!

var isMobile = window.innerWidth ");

editor91355.setValue(decodedContent); // Assign the initial text editor91355.clearSelection();

editor91355.setOptions({ maxLines: Infinity });

function decodeHTML91355(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard91355() { const code = editor91355.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor91355 .copymessage").show(); setTimeout(function() { jQuery(".maineditor91355 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode91355() { var code = editor91355.getSession().getValue();

jQuery("#runBtn91355 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post", data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output91355").html("

"+data+"");
        jQuery(".maineditor91355 .code-editor-output").show();
        jQuery("#runBtn91355 i.run-code").hide();

} })

}

function closeoutput91355() { var code = editor91355.getSession().getValue(); jQuery(".maineditor91355 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn91355").addEventListener("click", copyCodeToClipboard91355); document.getElementById("runBtn91355").addEventListener("click", runCode91355); document.getElementById("closeoutputBtn91355").addEventListener("click", closeoutput91355);

Output:

undefined reference error

Explanation: The code demonstrates how a function myFunction() has been declared but not defined, which results in an unresolved external symbol error during the linking phase because the linker cannot locate its implementation.

Reasons for Undefined Reference / Unresolved External Symbol Errors in C++

  1. When you declare a function but no linking files provide a definition for that function, you will encounter an undefined reference to that function.
  2. If the declaration and the definition of a function are mismatched, it will produce a linker error.
  3. In the absence of an object file containing a function definition, the linker cannot resolve the reference.
  4. If you are utilizing a static library and it may not have been linked properly, this could lead to an unresolved external symbol.
  5. Template and inline functions need to be defined in the header file to prevent linking issues.
  6. If a function is situated within a namespace but is called without clear specification, the linker will be unable to find it.
  7. A pure virtual function must be implemented in a derived class; otherwise, the linker will generate an error.

Solutions for Undefined Reference / Unresolved External Errors in C++

  1. Missing Function Definition: Ensure every declared function has an associated definition.
  2. Incorrect Function Signature: Verify that the function declaration and definition correspond correctly.
  3. Undefined Class Member Function: Supply a definition for ClassName::method().
  4. Static Library Not Linked: Confirm that the .a (GCC) or .lib (MSVC) file is linked prior to compilation.
  5. Virtual Function Not Defined: Implement all pure virtual functions of derived classes.
  6. Namespace Issues: Use Namespace::Function() or apply a directive to access functions.
  7. Inline and Template Issues: You must define inline and template functions properly to avoid linking errors.
    ``````html
    functions in header files. 

Frequent Scenarios and Solutions for Undefined Reference / Unresolved External Symbol Errors in C++

1. Absent Function Definition

An absent function definition is an issue that arises when a function is declared but lacks a corresponding definition in any of the linked files, leading the linker to produce an undefined reference error.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor32495.setValue(decodedContent); editor32495.clearSelection();

editor32495.setOptions({ maxLines: Infinity });

function decodeHTML32495(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard32495() { const code = editor32495.getValue(); navigator.clipboard.writeText(code).then(() => { jQuery(".maineditor32495 .copymessage").show(); setTimeout(function() { jQuery(".maineditor32495 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode32495() {

var code = editor32495.getSession().getValue();

jQuery("#runBtn32495 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post", data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output32495").html("

"+data+"");
			jQuery(".maineditor32495 .code-editor-output").show();
			jQuery("#runBtn32495 i.run-code").hide();
		}
	})

}

function closeoutput32495() { var code = editor32495.getSession().getValue(); jQuery(".maineditor32495 .code-editor-output").hide(); }

document.getElementById("copyBtn32495").addEventListener("click", copyCodeToClipboard32495); document.getElementById("runBtn32495").addEventListener("click", runCode32495); document.getElementById("closeoutputBtn32495").addEventListener("click", closeoutput32495);

Output with Error:

Fixes for Undefined Reference

Clarification: The preceding code specifies myFunction() but fails to provide a definition for it, causing the linker to be unable to locate its implementation, which results in an undefined reference error during the linking stage.

Resolution: Include the function definition.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor77908.setValue(decodedContent); editor77908.clearSelection();

editor77908.setOptions({ maxLines: Infinity });

function decodeHTML77908(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

function copyCodeToClipboard77908() { const code = editor77908.getValue(); navigator.clipboard.writeText(code).then(() => { jQuery(".maineditor77908 .copymessage").show(); setTimeout(function() { jQuery(".maineditor77908 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode77908() {

var code = editor77908.getSession().getValue();

jQuery("#runBtn77908 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ ``````javascript "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output77908").html("

"+data+"");
									jQuery(".maineditor77908 .code-editor-output").show();
									jQuery("#runBtn77908 i.run-code").hide();

} })

}

function closeoutput77908() { var code = editor77908.getSession().getValue(); jQuery(".maineditor77908 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn77908").addEventListener("click", copyCodeToClipboard77908); document.getElementById("runBtn77908").addEventListener("click", runCode77908); document.getElementById("closeoutputBtn77908").addEventListener("click", closeoutput77908);

Result:

Issues for Undefined Reference

Clarification: The provided code guarantees that the linker can locate the function's implementation effectively, thus fixing the error.

2. Incorrect Function Signature

An erroneous function signature is an issue that arises when the function declaration and definition do not correspond precisely, resulting in the linker failing to resolve the function invocation.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor74118.setValue(decodedContent); // Set the default text editor74118.clearSelection();

editor74118.setOptions({ maxLines: Infinity });

function decodeHTML74118(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to duplicate code to clipboard function copyCodeToClipboard74118() { const code = editor74118.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor74118 .copymessage").show(); setTimeout(function() { jQuery(".maineditor74118 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode74118() {

var code = editor74118.getSession().getValue();

jQuery("#runBtn74118 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output74118").html("

"+data+"");
									jQuery(".maineditor74118 .code-editor-output").show();
									jQuery("#runBtn74118 i.run-code").hide();

} })

}

function closeoutput74118() { var code = editor74118.getSession().getValue(); jQuery(".maineditor74118 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn74118").addEventListener("click", copyCodeToClipboard74118); document.getElementById("runBtn74118").addEventListener("click", runCode74118); document.getElementById("closeoutputBtn74118").addEventListener("click", closeoutput74118);

Error Output:

Erroneous Function Signature

Clarification: The code above results in an undefined reference error because the function declaration (void myFunction(int x);) anticipates an int parameter, yet the definition (void myFunction()) omits any parameters, consequently preventing the linker from aligning the function invocation myFunction(10) with the provided definition.

How to Rectify: Ensure the function definition corresponds with the declaration.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor7453.setValue(decodedContent); // Initialize the default text editor7453.clearSelection();

editor7453.setOptions({ maxLines: Infinity });

function decodeHTML7453(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard7453() { const code = editor7453.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor7453 .copymessage").show(); setTimeout(function() { jQuery(".maineditor7453 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode7453() { var code = editor7453.getSession().getValue();

jQuery("#runBtn7453 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output7453").html("

"+data+"");
            jQuery(".maineditor7453 .code-editor-output").show();
            jQuery("#runBtn7453 i.run-code").hide();
        }
    });
}

function closeoutput7453() { var code = editor7453.getSession().getValue(); jQuery(".maineditor7453 .code-editor-output").hide(); }

// Attach event listeners to the buttons document.getElementById("copyBtn7453").addEventListener("click", copyCodeToClipboard7453); document.getElementById("runBtn7453").addEventListener("click", runCode7453); document.getElementById("closeoutputBtn7453").addEventListener("click", closeoutput7453);

Output:

Fixes for Undefined Reference

Explanation: This snippet illustrates how a function definition aligns with its declaration, enabling the linker to resolve the function call without encountering any issues.

3. Issues with Inline Functions

The following are undefined reference errors that arise when inline functions are declared in one unit but not defined within the same unit, causing the linker to fail in detecting a definition for these inline functions.

Example:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor13084.setValue(decodedContent); // Initialize the default text editor13084.clearSelection();

editor13084.setOptions({ maxLines: Infinity });

function decodeHTML13084(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard13084() { const code = editor13084.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor13084 .copymessage").show(); setTimeout(function() { jQuery(".maineditor13084 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode13084() { var code = editor13084.getSession().getValue();

jQuery("#runBtn13084 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output13084").html("

"+data+"");
            jQuery(".maineditor13084 .code-editor-output").show();
            jQuery("#runBtn13084 i.run-code").hide();
        }
    });
}

function closeoutput13084() { var code = editor13084.getSession().getValue(); jQuery(".maineditor13084 .code-editor-output").hide(); }

// Attach event listeners to the buttons document.getElementById("copyBtn13084").addEventListener("click", copyCodeToClipboard13084); document.getElementById("runBtn13084").addEventListener("click", runCode13084); document.getElementById("closeoutputBtn13084").addEventListener("click", closeoutput13084);

Output with Error:

``````html
Inline Function Problems

Clarification: The provided code illustrates that the inline function myFunction() is declared but lacks a definition, leading to an error due to an undefined reference.

How to Resolve it: Provide the inline function's definition in either the same file or in a header file.

Sample:

Cpp

Code Duplicated!

var isMobile = window.innerWidth ");

editor24749.setValue(decodedContent); // Define the initial text editor24749.clearSelection();

editor24749.setOptions({ maxLines: Infinity });

function decodeHTML24749(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to duplicate code into clipboard function copyCodeToClipboard24749() { const code = editor24749.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { jQuery(".maineditor24749 .copymessage").show(); setTimeout(function() { jQuery(".maineditor24749 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error duplicating code: ", err); }); }

function runCode24749() {

var code = editor24749.getSession().getValue();

jQuery("#runBtn24749 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output24749").html("

"+data+"");
									jQuery(".maineditor24749 .code-editor-output").show();
									jQuery("#runBtn24749 i.run-code").hide();

} }) }

function closeoutput24749() { var code = editor24749.getSession().getValue(); jQuery(".maineditor24749 .code-editor-output").hide(); }

// Register event listeners to the buttons document.getElementById("copyBtn24749").addEventListener("click", copyCodeToClipboard24749); document.getElementById("runBtn24749").addEventListener("click", runCode24749); document.getElementById("closeoutputBtn24749").addEventListener("click", closeoutput24749);

Result:

Inline Function Problems

Clarification: The program exhibits how the compiler circumvents the linking error by replacing the inline definition calls to myFunction().

4. Template Function Concern

A template function must be defined within the same translation unit or included in a header file, since the compiler needs to produce its definition for every type utilized. In the absence of the definition, the linker will produce an undefined reference error.

Sample:

Cpp

Code Duplicated!

var isMobile = window.innerWidth ");

editor9604.setValue(decodedContent); // Set the initial text editor9604.clearSelection();

editor9604.setOptions({ maxLines: Infinity });

function decodeHTML9604(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to duplicate code into clipboard function copyCodeToClipboard9604() { const code = editor9604.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { jQuery(".maineditor9604 .copymessage").show(); setTimeout(function() { jQuery(".maineditor9604 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error duplicating code: ", err); }); } ``````javascript err); }); }

function executeCode9604() {

var sourceCode = editor9604.getSession().getValue();

jQuery("#runBtn9604 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: sourceCode, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var resultArray = response.split("~"); var outputData = resultArray[1];

jQuery(".output9604").html("

"+outputData+"");
									jQuery(".maineditor9604 .code-editor-output").show();
									jQuery("#runBtn9604 i.run-code").hide();

} })

}

function hideOutput9604() { var sourceCode = editor9604.getSession().getValue(); jQuery(".maineditor9604 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn9604").addEventListener("click", copyToClipboard9604); document.getElementById("runBtn9604").addEventListener("click", executeCode9604); document.getElementById("closeoutputBtn9604").addEventListener("click", hideOutput9604);

Output with Error:

Template Function Issue

Clarification: The provided code encounters an undefined reference issue because the template function add(T a, T b) is declared but lacks a definition, implying that the complete definition should reside in the same translation unit.

Resolution: Implement the template function in either the same file or a header file.

Illustration:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor6639.setValue(decodedContent); // Set the default text editor6639.clearSelection();

editor6639.setOptions({ maxLines: Infinity });

function decodeHTML6639(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyToClipboard6639() { const code = editor6639.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor6639 .copymessage").show(); setTimeout(function() { jQuery(".maineditor6639 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function executeCode6639() {

var sourceCode = editor6639.getSession().getValue();

jQuery("#runBtn6639 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: sourceCode, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var resultArray = response.split("~"); var outputData = resultArray[1];

jQuery(".output6639").html("

"+outputData+"");
									jQuery(".maineditor6639 .code-editor-output").show();
									jQuery("#runBtn6639 i.run-code").hide();

} })

}

function hideOutput6639() { var sourceCode = editor6639.getSession().getValue(); jQuery(".maineditor6639 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn6639").addEventListener("click", copyToClipboard6639); document.getElementById("runBtn6639").addEventListener("click", executeCode6639); document.getElementById("closeoutputBtn6639").addEventListener("click", hideOutput6639);

Output:

Template Function Issue fix

Clarification: The preceding code appropriately defines and utilizes a template function. The template function add(T a, T b) incorporates both a declaration and a definition within the same file, enabling the compiler to create the correct instantiation of the function when called with add(3, 4).

5. Virtual Function Not Implemented in Derived Class

A pure virtual function in a base class necessitates implementation in any derived class. The absence of this implementation results in an undefined reference error generated by the linker, leaving the class abstract and inhibiting the creation of objects.

Illustration:

Cpp

Code Copied!

``````html
class="code-editor-output-wrap output88793">

var isMobile = window.innerWidth “);

editor88793.setValue(decodedContent); // Assign the default text editor88793.clearSelection();

editor88793.setOptions({ maxLines: Infinity });

function decodeHTML88793(input) { var doc = new DOMParser().parseFromString(input, “text/html”); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard88793() { const code = editor88793.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert(“Code copied to clipboard!”);

jQuery(“.maineditor88793 .copymessage”).show(); setTimeout(function() { jQuery(“.maineditor88793 .copymessage”).hide(); }, 2000); }).catch(err => { console.error(“Error copying code: “, err); }); }

function runCode88793() {

var code = editor88793.getSession().getValue();

jQuery(“#runBtn88793 i.run-code”).show(); jQuery(“.output-tab”).click();

jQuery.ajax({ url: “https://intellipaat.com/blog/wp-admin/admin-ajax.php”, type: “post”,

data: { language: “cpp”, code: code, cmd_line_args: “”, variablenames: “”, action:”compilerajax” }, success: function(response) { var myArray = response.split(“~”); var data = myArray[1];

jQuery(“.output88793”).html(“

"+data+"");
									jQuery(".maineditor88793 .code-editor-output").show();
									jQuery("#runBtn88793 i.run-code").hide();

} })

}

function closeoutput88793() { var code = editor88793.getSession().getValue(); jQuery(".maineditor88793 .code-editor-output").hide(); }

// Bind event listeners to the buttons document.getElementById("copyBtn88793").addEventListener("click", copyCodeToClipboard88793); document.getElementById("runBtn88793").addEventListener("click", runCode88793); document.getElementById("closeoutputBtn88793").addEventListener("click", closeoutput88793);

Output with Error:

Virtual Function Not Defined in Derived Class

Explanation: The code leads to a linker error due to the pure virtual function display() being declared in the Base class without implementation in the Derived class. As Derived fails to provide a definition for display(), it remains categorized as an abstract class and cannot be instantiated.

Resolution: Implement the Pure Virtual Function in the Derived Class

Example:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor77134.setValue(decodedContent); // Assign the default text editor77134.clearSelection();

editor77134.setOptions({ maxLines: Infinity });

function decodeHTML77134(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard77134() { const code = editor77134.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor77134 .copymessage").show(); setTimeout(function() { jQuery(".maineditor77134 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode77134() {

var code = editor77134.getSession().getValue();

jQuery("#runBtn77134 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output77134").html("

"+data+"");
									jQuery(".maineditor77134 .code-editor-output").show();
									jQuery("#runBtn77134 i.run-code").hide();

} })

}

function closeoutput77134() { var code = editor77134.getSession().getValue(); jQuery(".maineditor77134 .code-editor-output").hide(); } ``````javascript // Attach event handlers to the buttons document.getElementById("copyBtn77134").addEventListener("click", copyCodeToClipboard77134); document.getElementById("runBtn77134").addEventListener("click", runCode77134); document.getElementById("closeoutputBtn77134").addEventListener("click", closeoutput77134);

Outcome:

Understanding Undefined Reference and Unresolved External Symbol Errors in C++

Clarification: The code effectively establishes a pure virtual function within the subclass, enabling successful compilation and operation.

6. Namespace and Scope Challenges

A function or variable defined within a namespace must be referred to using the namespace prefix unless a directive is in effect. Invoking the function without the appropriate prefix would prevent the linker from locating its definition, resulting in an unresolved reference error.

Instance:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor70195.setValue(decodedContent); // Set the default text editor70195.clearSelection();

editor70195.setOptions({ maxLines: Infinity });

function decodeHTML70195(input) { var doc = new DOMParser().parseFromString(input, "text/html"); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard70195() { const code = editor70195.getValue(); // Get code from the editor navigator.clipboard.writeText(code).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor70195 .copymessage").show(); setTimeout(function() { jQuery(".maineditor70195 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode70195() {

var code = editor70195.getSession().getValue();

jQuery("#runBtn70195 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post", data: { language: "cpp", code: code, cmd_line_args: "", variablenames: "", action: "compilerajax" }, success: function(response) { var myArray = response.split("~"); var data = myArray[1];

jQuery(".output70195").html("

" + data + "");
            jQuery(".maineditor70195 .code-editor-output").show();
            jQuery("#runBtn70195 i.run-code").hide();

} }) }

function closeoutput70195() { var code = editor70195.getSession().getValue(); jQuery(".maineditor70195 .code-editor-output").hide(); }

// Attach event handlers to the buttons document.getElementById("copyBtn70195").addEventListener("click", copyCodeToClipboard70195); document.getElementById("runBtn70195").addEventListener("click", runCode70195); document.getElementById("closeoutputBtn70195").addEventListener("click", closeoutput70195);

Output with Error:

 Pure Virtual Function in Derived Class

Clarification: The preceding code causes a linker error because certain operations are defined within the MyNamespace namespace but are invoked without using the namespace prefix in main(). Consequently, the compiler cannot locate myFunction() in the global scope, resulting in an undefined error.

Solution: Utilize the Namespace Prefix or Directive

Instance:

Cpp

Code Copied!

var isMobile = window.innerWidth ");

editor83575.setValue(decodedContent); ``````javascript // Clear the default text editor83575.clearSelection();

editor83575.setOptions({ maxLines: Infinity });

function decodeHTML83575(input) { var document = new DOMParser().parseFromString(input, "text/html"); return document.documentElement.textContent; }

// Function to duplicate code to clipboard function copyCodeToClipboard83575() { const codeSnippet = editor83575.getValue(); // Retrieve code from the editor navigator.clipboard.writeText(codeSnippet).then(() => { // alert("Code copied to clipboard!");

jQuery(".maineditor83575 .copymessage").show(); setTimeout(function() { jQuery(".maineditor83575 .copymessage").hide(); }, 2000); }).catch(err => { console.error("Error copying code: ", err); }); }

function runCode83575() { var codeSnippet = editor83575.getSession().getValue();

jQuery("#runBtn83575 i.run-code").show(); jQuery(".output-tab").click();

jQuery.ajax({ url: "https://intellipaat.com/blog/wp-admin/admin-ajax.php", type: "post",

data: { language: "cpp", code: codeSnippet, cmd_line_args: "", variablenames: "", action:"compilerajax" }, success: function(response) { var myArray = response.split("~"); var outputData = myArray[1];

jQuery(".output83575").html("

"+outputData+"");
            jQuery(".maineditor83575 .code-editor-output").show();
            jQuery("#runBtn83575 i.run-code").hide();
        }
    });
}

function closeoutput83575() { var codeSnippet = editor83575.getSession().getValue(); jQuery(".maineditor83575 .code-editor-output").hide(); }

// Attach event listeners to the buttons document.getElementById("copyBtn83575").addEventListener("click", copyCodeToClipboard83575); document.getElementById("runBtn83575").addEventListener("click", runCode83575); document.getElementById("closeoutputBtn83575").addEventListener("click", closeoutput83575);

Output:

 Namespace Qualifier or Directive

Explanation: The code invokes myFunction() with the appropriate namespace qualifier, enabling the compiler to locate and link the function without encountering any errors.

Conclusion

Undefined references and unresolved external symbol errors emerge when the linker is unable to locate a definition for a declared function or variable. Typically, these errors result from absent functions, incorrect function types, missing object files, and inadequately linked libraries. To resolve such issues, all functions must be properly defined, the function signatures and declarations must correspond, and all requisite files must be linked appropriately, along with the management of templates, inline functions, and namespaces.

What are undefined reference/unresolved external symbol errors in C++ – FAQs

The post What are undefined reference/unresolved external symbol errors in C++? appeared first on Intellipaat Blog.

```


Leave a Reply

Your email address will not be published. Required fields are marked *

Share This