I am trying to compile this piece of code on both Linux and Windows for a larger program. The following 3 files are for implementing a simple Linked List.
linkedList.h:
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType {
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedListType {
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmptyList();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
friend ostream& operator<< <Type> (ostream&, const linkedListType<Type>&);
};
template<class Type>
bool linkedListType<Type>::isEmptyList() {
return(first == NULL);
}
template<class Type>
linkedListType<Type>::linkedListType() {
first = NULL;
last = NULL;
count = 0;
}
template<class Type>
void linkedListType<Type>::destroyList() {
nodeType<Type> *temp;
while (first != NULL) {
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
void linkedListType<Type>::initializeList() {
destroyList();
}
template<class Type>
int linkedListType<Type>::length() {
return count;
}
template<class Type>
Type linkedListType<Type>::front() {
assert(first != NULL);
return first->info;
}
template<class Type>
Type linkedListType<Type>::back() {
assert(last != NULL);
return last->info;
}
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem) {
nodeType<Type> *current;
bool found;
current = first;
found = false;
while (current != NULL && !found)
if (current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem) {
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if (last == NULL)
last = newNode;
}
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem) {
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = NULL;
if (first == NULL) {
first = newNode;
last = newNode;
count++;
} else {
last->link = newNode;
last = newNode;
count++;
}
}
template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem) {
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if (first == NULL)
cerr << "Can not delete from an empty list.\n";
else {
if (first->info == deleteItem) {
current = first;
first = first->link;
count--;
if (first == NULL)
last = NULL;
delete current;
} else {
found = false;
trailCurrent = first;
current = first->link;
while (current != NULL && !found) {
if (current->info != deleteItem) {
trailCurrent = current;
current = current->link;
} else
found = true;
}
if (found) {
trailCurrent->link = current->link;
count--;
if (last == current)
last = trailCurrent;
delete current;
} else
cout << "Item to be deleted is not in the list." << endl;
}
}
}
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list) {
nodeType<Type> *current;
current = list.first;
while (current != NULL) {
osObject << current->info << " ";
current = current->link;
}
return osObject;
}
template<class Type>
linkedListType<Type>::~linkedListType() {
destroyList();
}
template<class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList) {
nodeType<Type> *newNode;
nodeType<Type> *current;
if (first != NULL)
destroyList();
if (otherList.first == NULL) {
first = NULL;
last = NULL;
count = 0;
} else {
current = otherList.first;
count = otherList.count;
first = new nodeType<Type>;
assert(first != NULL);
first->info = current->info;
first->link = NULL;
last = first;
current = current->link;
while (current != NULL) {
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
template<class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList) {
first = NULL;
copyList(otherList);
}
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList) {
if (this != &otherList)
copyList(otherList);
return *this;
}
#endif
linkedListForGraph.h:
#ifndef H_LinkedListForGraph
#define H_LinkedListForGraph
#include <iostream>
#include "linkedList.h"
using namespace std;
template<class vType>
class linkedListGraph: public linkedListType<vType> {
public:
void getAdjacentVertices(vType adjacencyList[], int& length);
};
template<class vType>
void linkedListGraph<vType>::getAdjacentVertices
(vType adjacencyList[], int& length) {
nodeType<vType> *current;
length = 0;
current = first;
while (current != NULL) {
adjacencyList[length++] = current->info;
current = current->link;
}
}
#endif
And a simple driver.cpp:
#include "linkedListForGraph.h"
int main(int argc, char const *argv[]) {
return 0;
}
When I'm compiling on windows using the MSVC command line tools using the command:
cl driver.cpp /Ehsc /O2
It works fine, and as expected I get the driver.exe with zero errors.
Now when I'm compiling on Linux using GCC using the command:
g++ driver.cpp -o driver -O3 -Wall -pedantic -Wshadow -Wextra
As well as clang:
clang++ driver.cpp -o driver -O3 -Wall -pedantic -Wshadow -Wextra
I get this weird error from gcc:
In file included from driver.cpp:1:0:
linkedListForGraph.h: In member function ‘void linkedListGraph<vType>::getAdjacentVertices(vType*, int&)’:
linkedListForGraph.h:21:15: error: ‘first’ was not declared in this scope
current = first;
^
clang outputs:
In file included from driver.cpp:1:
./linkedListForGraph.h:21:15: error: use of undeclared identifier 'first'
current = first;
^
I'm flummoxed with this error as I know that the inheritance is happening clearly and the variable first is accessible from the base class and since the MSVC gave no errors at all, I have no idea what could have I done wrong.
Any help is appreciated.