C++ 为什么会出现“无法从Dequeu转换为int”错误?
我目前正试图将我的第一个模板类作为C++类的赋值,但我不明白为什么我会一直犯这个错误:C++ 为什么会出现“无法从Dequeu转换为int”错误?,c++,class,templates,C++,Class,Templates,我目前正试图将我的第一个模板类作为C++类的赋值,但我不明白为什么我会一直犯这个错误: g++ -c main.cpp main.cpp: In function ‘int main(int, char**)’: main.cpp:12:14: error: cannot convert ‘Dequeu<int>’ to ‘int’ in initialization int ou = i[0]; dequeu.h: #ifndef MAIN_H #define MAIN_H
g++ -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:12:14: error: cannot convert ‘Dequeu<int>’ to ‘int’ in initialization
int ou = i[0];
dequeu.h:
#ifndef MAIN_H
#define MAIN_H
#endif
#include "Node.h"
#include <stddef.h> //NULL
template<typename T>
class Dequeu {
public:
Dequeu(); ~Dequeu();
void push_back(T);
T &operator[] (int i) {
if (i<size && i>=0){
//head?
if (i == 0)
return head->value;
//tail?
if (i == size-1)
return tail->value;
//other:
Node<T>* temp = head;
i--;
while (i != 0 ){
temp = temp->next;
i--;
}
return temp->Value();
}
}
private:
Node<T> * head;
Node<T> * tail;
int size;
};
template<typename T>
Dequeu<T>::Dequeu() {
head->nullify();
tail->nullify();
}
template<typename T>
Dequeu<T>::~Dequeu(){
Node<T>* temp = head;
while (temp->Next() != NULL) {
temp = temp->next;
delete(head);
head=temp;
}
}
template<typename T>
void Dequeu<T>::push_back(T t){
Node<T>* newNode;
newNode->Value(t);
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
if (head == NULL)
head = tail;
size++;
}
和节点h:
#include <stddef.h> //NULL
template <typename T>
class Node {
public:
Node<T>* prev;
Node<T>* next;
T value;
Node(); ~Node();
void nullify ();
private:
};
template <typename T>
void Node<T>::nullify() {
this->value = NULL;
this->next = NULL;
this->prev = NULL;}
我尝试的最后一件事是事件只返回这个->头->值,而不检查操作符[]中的输入整数
该类尚未完成,因此不必奇怪为什么只实现了两个函数
请随时告诉我如何更好地编写此代码,如果您发现其中有一些非常糟糕的地方,我在这方面非常糟糕。当您有Vector/Deque等容器时,没有特定的理由使用相同的指针*
Dequeu<int>* i = new Dequeu<int>();
int ou = i[0];
用止痛药就行了
而不是德克*
内置重载处理了几乎所有的事情您的主要问题,即编译错误,已经由TartanLlama回答了 但是,您也会问:如果您发现一些非常糟糕的地方,请随时告诉我如何更好地编写此代码,因此我将为其他部分添加此答案 似乎您在整个代码中都误解了指针的概念。当您定义指向某种类型元素的指针时,您将得到指向某种类型元素的指针,仅此而已!您将无法获得该类型的元素 例如:
SomeType* ptrA; // Just a pointer - it is not pointing to an instance of SomeType
// The pointer should never be used/dereferenced until
// it has been initialized to point to a real element.
SomeType* ptrB = new SomeType; // Now you have a pointer which points to
// an instance of SomeType.
// Now you can use the pointer to operate on the element.
查看您的一些代码:
template<typename T>
Dequeu<T>::Dequeu() {
head->nullify(); // Illegal. head is uninitialized and not pointing to a Node<T>
tail->nullify(); // Illegal. tail is uninitialized and not pointing to a Node<T>
}
代码通常如下所示:
template<typename T>
Dequeu<T>::Dequeu() {
head = nullptr;
tail = nullptr;
}
这里也有同样的问题:
template<typename T>
void Dequeu<T>::push_back(T t){
Node<T>* newNode; // newNode is just a pointer - there is no Node<T> element
newNode->Value(t); // Illegal - see above
newNode->prev = tail;
tail->next = newNode; // Illegal - tail may be nullptr
tail = newNode;
if (head == NULL)
head = tail;
size++;
}
你需要重新设计。比如:
template<typename T>
void Dequeu<T>::push_back(T t){
Node<T>* newNode = new Node<T>; // Create a new Node<T> and have newNode point at it
newNode->Value(t);
if (head == nullptr)
{
newNode->prev = nullptr;
newNode->next = nullptr;
head = newNode;
tail = newNode;
size = 1;
return;
}
if (tail == nullptr) throw some_exception....
// Add code to insert newNode at the back
size++;
}
int ou=*i[0]会这样做。i是一个Dequeu*:您必须取消引用它。[OT]:您必须在构造函数中初始化Dequeu成员。几乎所有代码都是无关的。你应该尝试创建一个MCVE。什么?。。。记住:C++新手,谢谢,但是使用Dequeu i=new Dequeu创建它;给我main.cpp:8:34:错误:从'Dequeu*'转换为非标量类型'Dequeu'请求的Dequeu i=new Dequeu;不要使用Dequeu i=new Dequeu;创建它;,使用Dequeu i;。这里不需要动态分配。我猜您来自Java背景。在爪哇和C++中的新概念完全不同。是的,来自java——我已经讨厌C++了。我必须处理循环引用,却不知道如何解决它——虽然有很多关于这个主题的帖子,但我知道..就像template class Node;。最好在使用节点的文件顶部进行前向声明,但不需要完整的定义,如头文件,其中声明了通过引用获取节点的函数。您所指的重载是什么?这很好,谢谢!但是我得到了这个nullptr没有在这个范围内声明的错误-如何解决?这是一个C++11的东西。如果您不使用C++11,只需使用NULL即可。@DoRe:nullptr在C++11中,因此在编译器标志中添加-std=C++11。
template<typename T>
Dequeu<T>::Dequeu() {
head = nullptr;
tail = nullptr;
}
template<typename T>
void Dequeu<T>::push_back(T t){
Node<T>* newNode; // newNode is just a pointer - there is no Node<T> element
newNode->Value(t); // Illegal - see above
newNode->prev = tail;
tail->next = newNode; // Illegal - tail may be nullptr
tail = newNode;
if (head == NULL)
head = tail;
size++;
}
template<typename T>
void Dequeu<T>::push_back(T t){
Node<T>* newNode = new Node<T>; // Create a new Node<T> and have newNode point at it
newNode->Value(t);
if (head == nullptr)
{
newNode->prev = nullptr;
newNode->next = nullptr;
head = newNode;
tail = newNode;
size = 1;
return;
}
if (tail == nullptr) throw some_exception....
// Add code to insert newNode at the back
size++;
}