Question: Compilation failed with errors: / usr / bin / ld: build / main . o: ( . bss + 0 x 0 ) : multiple

Compilation failed with errors:
/usr/bin/ld: build/main.o:(.bss+0x0): multiple definition of `DNode::activeNodes'; build/DNode.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make: ***[Makefile:20: build/main_185_44_2] Error 1
Recommendation: Check for recursive inclusion, unhandled exceptions, logical errors, or syntax issues in the code.Submission Status: Failed
------------------------------
=== Automatic Test Results ===
------------------------------
Compilation failed with errors:
/usr/bin/ld: build/main.o:(.bss+0x0): multiple definition of `DNode::activeNodes'; build/DNode.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make: ***[Makefile:20: build/main_185_44_2] Error 1Recommendation: Check for recursive inclusion, unhandled exceptions, logical errors, or syntax issues in the code.
pls help here is my code:
//DLinkedList.cpp
#include "DLinkedList.h"
#include
DLinkedList::DLinkedList() : head(nullptr), trailer(nullptr), listSize(0){}
DLinkedList::~DLinkedList(){
while (!empty()) removeFront();
}
bool DLinkedList::empty() const {
return head == nullptr;
}
const int& DLinkedList::front() const {
return head->elem;
}
const int& DLinkedList::back() const {
return trailer->elem;
}
void DLinkedList::addFront(const int& e){
DNode* newNode = new DNode(e, nullptr, head);
if (empty()){
head = trailer = newNode;
} else {
head->prev = newNode;
head = newNode;
}
listSize++;
}
void DLinkedList::addBack(const int& e){
DNode* newNode = new DNode(e, trailer, nullptr);
if (empty()){
head = trailer = newNode;
} else {
trailer->next = newNode;
trailer = newNode;
}
listSize++;
}
void DLinkedList::removeFront(){
if (empty()) return;
DNode* oldNode = head;
if (head == trailer){
head = trailer = nullptr;
} else {
head = head->next;
head->prev = nullptr;
}
delete oldNode;
listSize--;
}
void DLinkedList::removeBack(){
if (empty()) return;
DNode* oldNode = trailer;
if (head == trailer){
head = trailer = nullptr;
} else {
trailer = trailer->prev;
trailer->next = nullptr;
}
delete oldNode;
listSize--;
}
void DLinkedList::print(bool front) const {
if (empty()) return;
if (front){
DNode* node = head;
while (node != nullptr){
std::cout << node->elem <<"";
node = node->next;
}
} else {
DNode* node = trailer;
while (node != nullptr){
std::cout << node->elem <<"";
node = node->prev;
}
}
std::cout << std::endl;
}
int DLinkedList::size() const {
return listSize;
}
int DLinkedList::activeNodeCount() const {
return DNode::activeNodes;
}
//DNode.cpp
#include "DNode.h"
// Define the static member variable
int DNode::activeNodes =0;
DNode::DNode(int e, DNode* p, DNode* n) : elem(e), prev(p), next(n){
activeNodes++;
}
DNode::~DNode(){
activeNodes--;
}
//DLinkedList.h
#ifndef DLINKEDLIST_H
#define DLINKEDLIST_H
#include "DNode.h"
class DLinkedList {
private:
DNode* head; // Pointer to the first node
DNode* trailer; // Pointer to the last node
int listSize; // Stores the current number of elements in the list
public:
DLinkedList(); // Constructor
~DLinkedList(); // Destructor
bool empty() const; // Check if the list is empty
const int& front() const; // Get front element
const int& back() const; // Get back element
void addFront(const int& e); // Add to the front of the list
void addBack(const int& e); // Add to the back of the list
void removeFront(); // Remove front item from the list
void removeBack(); // Remove back item from the list
void print(bool front = true) const; // Print the list
int size() const; // Returns the current number of elements in the list
int activeNodeCount() const; // Return the count of active DNodes
};
#endif // DLINKEDLIST_H
//main.cpp
#include "DLinkedList.h"
#include
int main(){
DLinkedList list;
list.addFront(10);
list.addFront(20);
list.addFront(30);
list.addBack(40);
list.addBack(50);
std::cout << "List from head: ";
list.print(true);
std::cout << "List from trailer: ";
list.print(false);
list.removeFront();
list.removeBack();
std::cout << "List after removals: ";
list.print(true);
list.addFront(60);
list.addBack(70);
std::cout << "List after adding more elements: ";
list.print(true);
std::cout << "Active nodes: "<< list.activeNodeCount()<< std::endl;
return 0;
}
//DNode.h
#ifndef DNODE_H
#define DNODE_H
class DNode {
private:
int elem; // Element stored in the node
DNode* prev; // Pointer to the previous node
DNode* next; // Pointer to the next node
public:
static int activeNodes; // Sta

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!