为什么无法识别此模板类拥有的类的类型(使用模板类型)
Why is the type of this template-class-owned class, using a template type, unrecognized?
我希望这以前没有被某个问题所涵盖。我尽我所能地看起来,但我认为首先的部分问题在于我不明白发生了什么,这可能阻止了我找到以前的答案。如果是这样,我很抱歉,但除此之外...
为了练习模板并更好地理解C++和代码设计,我开始编写一个(目前非常简单的)链表实现,主要是为了模仿 std::list。我一直在努力正确实现迭代器,并在逻辑上实现其他组件,但我遇到了一个障碍。我猜它是在某处的模板语法,但我不确定。这可能只是一些愚蠢的错误。
下面是该类的一般结构:
template <class T>
class LinkedList {
public:
LinkedList();
class Iterator;
void push_front(const T&);
void push_back(const T&);
void pop_front();
void pop_back();
T& front();
T& back();
unsigned int size() const;
bool empty() const;
Iterator begin();
Iterator end();
private:
struct ListNode;
ListNode* m_front;
ListNode* m_back;
unsigned int m_size;
};
template <class T>
class LinkedList<T>::Iterator {
public:
Iterator();
Iterator(const Iterator& rhs);
Iterator(ListNode* const& node);
Iterator operator=(const Iterator& rhs);
T& operator*();
bool operator==(const Iterator& rhs) const;
bool operator!=(const Iterator& rhs) const;
Iterator operator++();
private:
ListNode* m_node;
};
template <class T>
struct LinkedList<T>::ListNode {
T* m_data;
ListNode* m_next;
};
这是有问题的函数:
template <class T>
void LinkedList<T>::push_front(const T&) {
if (m_front == NULL) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = NULL;
m_back = m_front;
} else if (m_front == m_back) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = m_back;
} else {
ListNode* former_front(m_front);
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = former_front;
}
}
以及GCC 4.6.3给出的错误:
linkedlist.hpp: In member function ‘void pract::LinkedList<T>::push_front(const T&)’:
linkedlist.hpp:75:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:80:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:85:31: error: expected primary-expression before ‘;’ token
我希望这一切都有所帮助,但如果还有其他需要,请问。谢谢大家。
问题出在以下几行:
*(m_front->m_data) = T;
这是试图为变量赋值类型,这显然是不可能的。可能你想要一个命名参数并将所述参数用于此赋值:
template <class T>
void LinkedList<T>::push_front(const T& t) {
if (m_front == NULL) {
m_front = new ListNode;
*(m_front->m_data) = t;
m_front->m_next = NULL;
m_back = m_front;
} else if (m_front == m_back) {
m_front = new ListNode;
*(m_front->m_data) = t;
m_front->m_next = m_back;
} else {
ListNode* former_front(m_front);
m_front = new ListNode;
*(m_front->m_data) = t;
m_front->m_next = former_front;
}
}
相关文章:
- std::visit无法识别类型
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- 无法在VSCode中设置C++调试;无法识别调试类型
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- C++将 wwn 字符串转换为识别为十六进制的数据类型
- 编写完整专业化以识别void类型的问题
- 无法识别协变返回类型
- 我该如何允许类型的对象非专业化并在不使用指针的情况下识别何时是这种情况
- 为什么模板<类型名...>不能通过模板<模板类型名>识别为可实例化<typename>?
- CGAL 无法识别类型
- 为什么我的Visual Studio 2017编译器无法识别C++17类型推导指南
- 当例外处理时,类型识别如何工作
- 无法识别初始化类型
- 从基类识别派生的类类型
- ActiveMQCPP - 识别测量类型
- 运算符 + 无法识别类型
- 无法识别'regex'类型
- 嵌套成员类型识别
- C++运行时数据类型识别并避免切换大小写