我正在尝试在Linux和Windows上编译这段代码以获得更大的程序。以下3个文件用于实现简单的链接列表。
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
一个简单的 driver.cpp :
#include "linkedListForGraph.h"
int main(int argc, char const *argv[]) {
return 0;
}
当我使用MSVC命令行工具在Windows上编译时使用以下命令:
cl driver.cpp /Ehsc /O2
它工作正常,正如预期的那样,我得到的驱动程序没有错误。
现在,当我使用GCC使用GCC在Linux上编译时:
g++ driver.cpp -o driver -O3 -Wall -pedantic -Wshadow -Wextra
以及clang:
clang++ driver.cpp -o driver -O3 -Wall -pedantic -Wshadow -Wextra
我从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输出:
In file included from driver.cpp:1:
./linkedListForGraph.h:21:15: error: use of undeclared identifier 'first'
current = first;
^
我对这个错误感到困惑,因为我知道继承发生得很清楚,变量首先可以从基类访问,因为MSVC根本没有给出任何错误,我不知道我做错了什么。
感谢任何帮助。
答案 0 :(得分:1)
写
current = this->first;