为什么我会收到"cannot convert from Dequeu<int> to int"错误?

Why am I getting "cannot convert from Dequeu<int> to int" error?

本文关键字:int lt gt 错误 to from cannot 为什么 convert Dequeu      更新时间:2023-10-16

我目前正试图编写我的第一个模板类作为我的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++;
}