[英]How to access a structure declared inside of a class?
我有實現節點結構的鏈表 class,如下所示:
template<class T>
class LinkedList
{
public:
struct Node {
T value;
Node *next;
};
int Length;
Node *head;
Node *tail;
LinkedList() {
Length = 0;
Node* head = nullptr;
Node* tail = nullptr;
}
};
我嘗試從驅動程序文件中訪問節點Node
結構,如下所示:
#include "LinkedList.h"
template<class T>
void foo(LinkedList<T> list) {
LinkedList<T>::Node* a = list.head; // does not work
LinkedList<int>::Node* b = list.head; // works (if T is int of course)
}
使用模板 T 不起作用(它給我“找不到標識符”錯誤消息),而直接指定正確的數據類型是有效的。 這是為什么? 有沒有辦法避免錯誤?
使用typename LinkedList<T>::Node* a =...
問題是不知道T
到底是什么,編譯器不能確定LinkedList<T>::Node
確實是一種類型( LinkedList
可以專門用於T
所以定義沒有幫助)。 您需要指示它以這種方式對待它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.