【问题标题】:Segmentation Fault in template operator overload模板运算符重载中的分段错误
【发布时间】:2017-03-28 19:58:00
【问题描述】:

我目前正在尝试掌握 C++ 中的模板和运算符重载。我写了一个类,它基本上是一个向量的包装器。我正在尝试将运算符重载添加到模板类中,以便可以遍历列表,但我不断收到重载运算符的分段错误。

这是模板代码。

template <class T> class List {
private:
    std::list<T *> *objects;
    int count;
    uint currentIndex;
    typename std::list<T*>::iterator item_iter;

public:
    T *currentItem;
    T *get(int idx);
    void add(T *obj);
    void start();
    bool empty();
    void next();
    List<T>& operator++();

    List() {
        this->currentItem = NULL;
        this->currentIndex = 0;
        this->count = 0;
        this->objects = new std::list<T *>;
    }

    ~List() {
        delete this->objects;
    }
};

template <class T>
void List<T>::add(T *obj) {
    this->objects->push_back(obj);
}

template <class T>
T *List<T>::get(int idx) {
    if (idx < this->objects->size()) {
      typename std::list<T*>::iterator it = this->objects->begin();
      std::advance(it, idx);
      return *it;
    } else {
      return NULL;
    }
}

template <class T>
void List<T>::start() {
    this->currentIndex = 0;
    if (this->objects->size() > 0) {
      this->item_iter = this->objects->begin();
      this->currentItem = *(this->item_iter);
    } else {
      this->currentItem = NULL;
    }
}

template <class T>
void List<T>::next() {
     if (!this->empty() && (this->currentIndex == 0) && (this->currentItem == NULL)) {
      this->start();
    } else if (this->currentIndex < this->objects->size() - 1) {
      this->currentIndex++;
      std::advance(item_iter, 1);
      this->currentItem = *item_iter;
    } else {
      this->currentItem = NULL;
    }
}

template <class T>
bool List<T>::empty() {
    return !this->objects->size();
}

template <class T>
List<T>& List<T>::operator++() {
    // Note: We don't get this far
    this->next();
    return *this;
}

以下代码有效。

List<Item> *items = new List<Item>();
items->addItem(new Item(20));

for(items->start(); items->currentItem != NULL; items->next()) {
    // Can work with items->currentItem;
}

以下代码导致分段错误。

List<Item> *items = new List<Item>();
items->addItem(new Item(20));

for(items->start(); items->currentItem != NULL; items++) {
    // Segmentation fault occurs before we get inside
}

【问题讨论】:

  • for(items-&gt;start(); items-&gt;currentItem != NULL; items++) 中,变量items 是一个指针,所以items++ 递增指针。它不再指向有效实例。你的意思可能是(*iter)++
  • 你为什么过度使用new? C++ 不是 Java。你有大量的内存泄漏。
  • std::list&lt;T *&gt; *objects; 不要那样做。就像,永远。这里根本不需要指针。 T *currentItem;不要。 void start();不要。 void next();不要。这种界面非常很糟糕。即使是为了学习也不要这样做。
  • {List&lt;int&gt; list1; List&lt;int&gt; list2 = list1;} -- 如果要更多的孔,这个两行程序有双重删除错误。
  • @FrançoisAndrieux 你是对的,我需要 (*items)++ 并且运算符重载需要接受 int。

标签: c++ templates operator-overloading


【解决方案1】:

@FrançoisAndrieux 是正确的。

我需要取消引用项目指针。

for(items.start(); items.currentItem != NULL; ++items) {
    // Can now use current item
}

我也改变了返回类型。

template <class T>
List<T> List<T>::operator++() {
    this->next();
    return *this;
}

【讨论】:

  • operator++(int)operator++() 是不同的运算符。 operator++() 是前增量运算符 (++i) 而operator++(int) 是后增量运算符 (i++)。后增量运算符应返回List&lt;T&gt; 而不是List&lt;T&gt;&amp;。如果你想使用operator++(),你可以使用++(*items)Link.
  • @FrançoisAndrieux 谢谢,我会改的。
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
相关资源
最近更新 更多