2016-04-17 3 views
-1

私は両端キューのポインタによって指されているオブジェクトを削除しようとしています。これは正しい方法ですか? valgrindでメモリリークをデバッグしようとしていますが、この両端キューが問題の原因になっているようです。C++、dequeでオブジェクトを削除するには?

std::deque<myObject *>::iterator it = myDeque.begin(); 

    while (it != myDeque.end()) 
    delete *it++; 
+0

ブラケットを使用すると、 –

+1

http://stackoverflow.com/help/mcve –

+2

@DendiSuhubdy 'しばらく(!それ= myDeque.end())' の後に括弧を必要とする仲間t。 –

答えて

-1

http://melpon.org/wandbox/permlink/O1sewWdaSEXrTZB1

あなたのコードと本質的に何も悪いことはありません。このコードは動作します:いいえ、あなたは「ドン:

#include <iostream> 
#include <deque> 

using namespace std; 

struct myObject{ 
    ~myObject(){cout<<"destructor"<<endl;} 
}; 

int main(){ 
    std::deque<myObject*> myDeque{new myObject, new myObject}; 
auto it = myDeque.begin(); 

    while (it != myDeque.end()) 
    delete *it++; 

} 
関連する問題