This question already has answers here:
Derived template-class access to base-class member-data

(3个答案)


6年前关闭。




我正在尝试在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

它工作正常,并且按预期方式我得到了零错误的driver.exe。

现在,当我使用GCC在Linux上使用以下命令进行编译时:
g++ driver.cpp -o driver -O3 -Wall -pedantic -Wshadow -Wextra

以及c声:
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;
               ^

lang输出
In file included from driver.cpp:1:
./linkedListForGraph.h:21:15: error: use of undeclared identifier 'first'
    current = first;
              ^

我被这个错误弄糊涂了,因为我知道继承很明显,并且可以从基类中首先访问变量,并且由于MSVC完全没有错误,所以我不知道我该怎么做。

任何帮助表示赞赏。

最佳答案


current = this->first;

关于c++ - 无法从基类C++访问变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24513553/

10-09 13:30