这是一个想法,我有一个用类型模板初始化的类 .

#ifndef DEQUE_H_
#define DEQUE_H_


class UnderflowError
{

};

class OverflowError
{

};

#include <iostream>
template < typename T >
class Deque
{
public:
    Deque< T >() : size_(), capacity_(5)
    {

    }
    Deque< T >(const Deque & dq) : size_(dq.size_), capacity_(dq.capacity_), x_(dq.x_)
    {

    }
    int operator[](int) const;
    int size() const;
    Deque & insert_tail(int x);
    Deque & insert_head(int);
    Deque & remove_tail();
    Deque & remove_head();
private:
    T * x_;
    int size_;
    int capacity_;
};

template < typename T >
std::ostream & operator<< (std::ostream & cout, const Deque<T>& dq);


#endif

然后初始化这些函数原型的源文件是这样的 .

#include <iostream>
#include "Deque.h"


template< typename T >
int Deque<T>::size() const
{
    return size_;
}

template< typename T >
int Deque<T>::operator[](int i) const
{
    return x_[i];
}

template< typename T >
Deque< T > & Deque< T >::insert_tail(int x)
{
    if (size_ == -1 || size_ == 0)
    {
        size_ = 1;
        x_[0] = x;
        return *this;
    }
    else if (size_ >= 0 && size_ < 5)
    {
        size_ += 1;
        x_[size_ - 1] = x;
        return *this;
    }
    else if (size_ == 5)
    {
        size_ = 5;
        T array[size_];
        array[0] = x_[1];
        x_[4] = x;
        for (int i = 1; i <= 3; i++)
        {
            array[i] = x_[i + 1];
        }
        for (int i = 0; i < 5; i++)
        {
            x_[i] = array[i];
        }
        return *this;
    }
}

template< typename T >
Deque< T > & Deque< T >::insert_head(int x)
{
    if (size_ == -1)
    {
        size_ = 1;
        x_[0] = x;
        return *this;
    }
    else if (size_ >= 0 && size_ < 5)
    {
        size_ += 1;
        T array[size_];
        for (int i = 0; i < size_; i++)
        {
            array[i] = x_[i];
        }
        x_[0] = x;
        for (int i = 1, j = 0; i < size_; i++, j++)
        {
            x_[i] = array[j];
        }
        return *this;
    }
    if (size_ == 5)
    {
        size_ = 5;
        T array[size_];
        array[0] = x;
        array[4] = array[3];
        for (int i = 1; i <= 3; i++)
        {
            array[i] = x_[i - 1];
        }
        for (int i = 0; i < 4; i++)
        {
            x_[i] = array[i];
        }
        return *this;
    }
}

template< typename T >
Deque< T > & Deque< T >::remove_tail()
{
    if (size_ < 0)
        {
            size_ = -1;
            return *this;
        }
        else if (size_ >= 0 && size_ < 5)
        {
            size_ -= 1;
            return *this;
        }
        if (size_ == 5)
        {
            T array[4];
            for (int i = 0; i < 4; i++)
            {
                array[i] = x_[i];
            }
            for (int i = 0; i < 4; i++)
            {
                x_[i] = array[i];
            }
            size_ = 4;
            return *this;
        }
}


template< typename T >
T & Deque< T >::remove_head()
{
    T thing = x_[0];
    if (size_ < 0)
    {
        size_ -= 1;
    }
    else if (size_ >= 0 && size_ < 5)
    {
        int size = size_ - 1;
        T array[size];
        for (int i = 0, j = 1; i < size; ++i, ++j)
        {
            array[i] = x_[j];
        }
        size_ -= 1;
        for (int i = 0; i < size_; ++i)
        {
            x_[i] = array[i];
        }
    }
    if (size_ == 5)
    {
        int size = size_ - 1;
        T array[size];
        for (int i = 0, j = 1; i < size; i++, ++j)
        {
            array[i] = x_[j];
        }
        for (int i = 0; i < size; ++i)
        {
            x_[i] = array[i];
        }
        size_ -= 1;
    }

    return thing;
}

template< typename T >
std::ostream & operator<< (std::ostream & cout, const Deque<T> & dq)
{
    cout << "[ ";
    for (int i = 0; i < dq.size(); ++i)
    {
        cout << dq[i] << " ";
    }
    cout << "]";

    return cout;
}

问题出在我想要如何利用删除头 . 这个想法非常简单,我想使用我“删除”的值,并将其分配给main中的整数变量 . J取6并且给定的数组假设有5和7但是不是那样,整个东西被删除了,就像首先没有添加任何内容一样 .

#include <iostream>
#include "Deque.h"
#include "Deque.cpp"

int main()
{
    Deque< int > dq;
    dq.insert_head(6);
    dq.insert_tail(5);
    dq.insert_head(7);

    std::cout << dq << std::endl;
    Deque< int >gq(dq);
    std::cout << gq << std::endl;

    Deque< int >dq2;
    dq2 = dq;
    std::cout << dq2 << std::endl;
    std::cout << dq << std::endl;

    int j = dq.remove_head();
    std::cout << dq << std::endl;

    return 0;
}

如果我注释掉该方法,则添加的所有值仍然存在 . 删除注释以便使用该方法后,j没有返回值,并且控制台中没有任何内容显示 . 我的问题是,给定方法的原因是什么?不应该是参考和给定数据类型同时实现返回值和从数组“删除”的事实?