为什么我会收到"cannot convert from Dequeu<int> to int"错误?
Why am I getting "cannot convert from Dequeu<int> to int" error?
我目前正试图编写我的第一个模板类作为我的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];
main.cpp:
#include "main.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main (int args, char ** argc){
Dequeu<int>* i = new Dequeu<int>();
i->push_back (10);
int ou = i[0];
cout<<"i[0]: "<<ou<<endl;
}
带main.h:
#include "Dequeu.h"
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++;
}
和Node.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;}
我尝试的最后一件事是事件只返回this->head->value
,而不检查运算符[]中的输入整数。
这个类还没有完成,所以不要奇怪为什么只有两个函数实现。。。
如果你发现这个代码很糟糕,请随时告诉我如何更好地编写它,我在这方面真的很糟糕。
Dequeu<int>* i = new Dequeu<int>();
int ou = i[0];
由于i
是一个指针,所以i[0]
的不是意味着在Dequeu<int>
上调用operator[]
,它本质上与*i
相同。
你的意思是int ou = (*i)[0];
,但实际上i
一开始就不应该是指针,你应该这样创建它:
Dequeu<int> i;
当您有Vector/Deque等容器时,没有特定的理由使用相同的指针(*)
只要用疼痛Deque而不是Deque*
内置的过载几乎可以处理的所有事务
TartanLlama已经回答了您的主要问题,即编译错误。
然而,你也会问:"如果你发现一些非常糟糕的东西,请随时告诉我如何更好地编写这段代码",所以我会在其他部分添加这个答案。
在整个代码中,您似乎误解了指针的概念。当您定义a pointer to an element of some type
时,您将得到a pointer to an element of some type
,仅此而已!您将不会得到an element of that type
。
示例:
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++;
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 呼叫运营商<<临时
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 运营商&lt;&lt;无法访问班级的私人int
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串