gpt4 book ai didi

c++ - 无法从基类 C++ 访问变量

转载 作者:行者123 更新时间:2023-12-02 11:09:25 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Derived template-class access to base-class member-data

(3 个回答)


7年前关闭。




我正在尝试在 Linux 和 Windows 上为更大的程序编译这段代码。以下 3 个文件用于实现一个简单的链表。

链接列表.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

还有一个简单的 驱动程序.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

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

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

我对这个错误感到困惑,因为我知道继承发生得很清楚,并且变量 first 可以从基类访问,并且由于 MSVC 根本没有给出错误,我不知道我做错了什么。

任何帮助表示赞赏。

最佳答案


current = this->first;

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com