Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help debugging this error: In file included from main.cpp : 4 : LinkedList.h : In instantiation of void LinkedList::PrintReverse ( ) [ with T

Need help debugging this error:
In file included from main.cpp : 4 :
LinkedList.h : In instantiation of void LinkedList::PrintReverse()[with T = int] :
main.cpp : 35 : 20 : required from here
LinkedList.h : 135 : 18 : error : ISO C++ forbids variable length array[-Wvla]
135| T listdata[this->NodeCount()];
|^ ~~~~~~~
Header file:
#include
#include
using namespace std;
template
struct Node {
Node * next;
T data;
};
template class LinkedList {
public:
Node * first;
Node * last;
LinkedList (){
first = NULL;
last = NULL;
}
void AddHead(T data){
if (first == NULL){
Node* temp = new Node();
temp->data = data;
temp->next = NULL;
first = temp;
last = temp;
}
else {
Node* temp = new Node;
temp->data = data;
temp->next = first;
first = temp;
}
}
void AddTail(T data){
if (last == NULL){
Node* temp = new Node();
temp->data = data;
temp->next = NULL;
first = temp;
last = temp;
}
else {
Node* temp = new Node();
temp->data = data;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void AddNodesHead(T data[], int size){
for (int i =0; i < size; i +=1){
this->AddHead(data[i]);
}
}
void AddNodesTail(T data[], int size){
for (int i =0; i < size; i +=1){
this->AddTail(data[i]);
}
}
int NodeCount(){
if (first == NULL){
return 0;
}
Node* temp = first;
int count =0;
while (temp != last){
count +=1;
temp = temp->next;
}
return count;
}
void PrintForward(){
if (first == NULL){
cout << "List IS empty!!!" << endl;
}
Node* temp = first;
do {
cout << temp->data <<"->";
temp = temp->next;
} while (temp != last);
cout << "NULL" << endl;
}
/// ISSUE AREA
void PrintReverse(){
if (first == NULL){
cout << "List IS empty!!!" << endl;
}
Node* temp = first;
T listdata[this->NodeCount()]; // Needs to not be a VLA
int i =0;
do {
listdata[i]= temp->data;
i +=1;
temp = temp->next;
} while (temp != last);
for (int i = this->NodeCount()-1; i >=0; i -=1){
cout << listdata[i]<<"->";
}
cout << "NULL" << endl;
}
~LinkedList(){
Node *temp;
while (temp != last){
temp = first;
first = first->next;
free(temp);
}
free(first);
free(last);
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions