为什么我得到&#34;无法从Dequeu <int>转换为int&#34;错误?

时间:2016-01-05 09:16:32

标签: 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];

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;
}

with 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而不检查operator []中的输入整数。

课程还没有完成,所以不要错过为什么只有两个功能实现...

请随时告诉我如何更好地编写此代码如果发现其中的内容非常糟糕,我真的很糟糕。

3 个答案:

答案 0 :(得分:6)

Dequeu<int>* i = new Dequeu<int>();
int ou = i[0];

由于i是一个指针,i[0] 意味着在operator[]上调用Dequeu<int>,它与{{1}基本相同}}

你的意思是*i,但真的int ou = (*i)[0];首先不应该是一个指针,你应该像这样创建它:

i

答案 1 :(得分:1)

当你有像Vector / Deque等容器时,没有特别的理由使用相同的指针(*)

只是使用疼痛Deque 而不是Deque *

内置的重载几乎可以处理所有事情

答案 2 :(得分:1)

你的主要问题,即编译错误,已由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++;
}