可能重复:
我何时应该在C++中使用new关键字?
我什么时候应该在C++中使用"new"运算符?我来自C#/ Java背景,实例化对象让我感到困惑.
如果我创建了一个名为"Point"的简单类,当我创建一个点时,我应该:
Point p1 = Point(0,0);
Run Code Online (Sandbox Code Playgroud)
要么
Point* p1 = new Point(0, 0);
Run Code Online (Sandbox Code Playgroud)
有人可以为我澄清何时使用新操作员以及何时不使用?
重复:
有关:
有人可以帮我弄清楚我在哪里得到这个错误.我知道它可能是双重删除或类似的东西.对于背景,这是霍夫曼树的一个实现,你可以在维基百科上轻松实现.
int main()
{
ifstream input;
input.open("input.txt");
MinPriorityQueue<CharCountNode> heap;
map<char, int> m;
while(input.good())
m[input.get()] += 1;
for( map<char, int>::const_iterator it = m.begin(); it != m.end(); ++it )
heap.enqueue(CharCountNode(it->first, it->second));
while(heap.getSize() > 1)
{
CharCountNode a, b, parent;
a = heap.dequeue();
b = heap.dequeue();
parent = CharCountNode('*', a.getCount() + b.getCount());
parent.left = &a;
parent.right = &b;
heap.enqueue(parent);
}
}
Run Code Online (Sandbox Code Playgroud)