【问题标题】:ostream printing memory map then dumping coreostream 打印内存映射然后转储核心
【发布时间】:2012-11-01 21:49:50
【问题描述】:

我有一个程序正在尝试实现优先级队列。在 PriorityQueue.h 文件中,我重载了 ostream 运算符

这是输出队列内容的调用(此行位于我的 main.cpp 文件中);

cout << "Printing Q1" << endl;
cout << Q1 << endl;

其中 Q1 是我的优先级队列,依次包含值 10、5 和 1。

这是我的 PriorityQueue.h 文件,底部是我定义重载 ostream 运算符的位置。

#include "ListT.h"
#include <ostream>
using namespace std;

//template <typename T>
template <typename T>
class PriorityQueue : private List<T>
{

public:

//constructor
PriorityQueue();
//add item to priority queue
void enqueue(T item);
//remove the item with the highest priority from the list
void dequeue();
//remove the item with the highest priority and assign its value to T& item
void dequeue(T& item);
//look at the item with highest priority and assign its value to T& item
void peek(T& item) const;
//return the size of the priority queue
int getSize() const;

//overloaded assignment operartor, performs deep copy of rhs
PriorityQueue<T>& operator =(PriorityQueue<T>& rhs);
};



//overloaded output operator
template <typename T>
ostream& operator <<(ostream& os, const PriorityQueue<T>& rhs)
{
    T item;
    PriorityQueue<T> rhsCopy = rhs;
    while(rhsCopy.getSize() > 0){
        rhsCopy.dequeue(item);
        os << item << endl;
    }

    return os;
}

//overloaded assignment operator/copy constructor
template <typename T>
PriorityQueue<T>& PriorityQueue<T>::operator =(PriorityQueue<T>& rhs) 
{
    int index = 1, size = rhs.getSize();
    T itemCopy;
    while(index < size){
        rhs->retrieve(index,itemCopy);  
        *this->enqueue(itemCopy);
        index++;
    }
    return *this;
}

#include "PriorityQueue.cpp"

编辑:

这是程序的输出

********* TEST 1 ***********************************************
Printing Q1
10  
5
1

*** glibc detected *** ./lab8: double free or corruption (fasttop): 0x000000000132a030 ***
======= Backtrace: =========
/lib64/libc.so.6[0x397c27bfee]
./lab8[0x4020b7]
./lab8[0x401929]
./lab8[0x402447]
./lab8[0x4014e8]
./lab8[0x401430]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x397c221735]
./lab8[0x401339]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:01 19792231                          
 /mnt/sda1    /c++/school/lab8/lab8/lab8
00603000-00604000 rw-p 00003000 08:01 19792231                            
/mnt/sda1    /c++/school/lab8/lab8/lab8
0132a000-0134b000 rw-p 00000000 00:00 0                                  
[heap]
397be00000-397be20000 r-xp 00000000 fd:01 165912                         
/usr/lib64    /ld-2.15.so
397c01f000-397c020000 r--p 0001f000 fd:01 165912                         
/usr/lib64/ld-2.15.so
397c020000-397c021000 rw-p 00020000 fd:01 165912                         
/usr/lib64/ld-2.15.so
397c021000-397c022000 rw-p 00000000 00:00 0 
397c200000-397c3ac000 r-xp 00000000 fd:01 165944                         
/usr/lib64/libc-2.15.so
397c3ac000-397c5ac000 ---p 001ac000 fd:01 165944                        
/usr/lib64/libc-2.15.so
397c5ac000-397c5b0000 r--p 001ac000 fd:01 165944                         
/usr/lib64/libc-2.15.so
397c5b0000-397c5b2000 rw-p 001b0000 fd:01 165944                         
/usr/lib64/libc-2.15.so
397c5b2000-397c5b7000 rw-p 00000000 00:00 0 
397d200000-397d2fa000 r-xp 00000000 fd:01 165973                         /usr/lib64/libm-2.15.so
397d2fa000-397d4f9000 ---p 000fa000 fd:01 165973                         /usr/lib64/libm-2.15.so
397d4f9000-397d4fa000 r--p 000f9000 fd:01 165973                         /usr/lib64/libm-2.15.so
397d4fa000-397d4fb000 rw-p 000fa000 fd:01 165973                         /usr/lib64/libm-2.15.so
3980a00000-3980a15000 r-xp 00000000 fd:01 165976                         /usr/lib64/libgcc_s-4.7.2-20120921.so.1
3980a15000-3980c14000 ---p 00015000 fd:01 165976                         /usr/lib64/libgcc_s-4.7.2-20120921.so.1
3980c14000-3980c15000 rw-p 00014000 fd:01 165976                         /usr/lib64/libgcc_s-4.7.2-20120921.so.1
3988a00000-3988ae5000 r-xp 00000000 fd:01 155500                         /usr/lib64/libstdc++.so.6.0.17
3988ae5000-3988ce4000 ---p 000e5000 fd:01 155500                         /usr/lib64/libstdc++.so.6.0.17
3988ce4000-3988cec000 r--p 000e4000 fd:01 155500                         /usr/lib64/libstdc++.so.6.0.17
3988cec000-3988cee000 rw-p 000ec000 fd:01 155500                         /usr/lib64/libstdc++.so.6.0.17
3988cee000-3988d03000 rw-p 00000000 00:00 0 
7fed62cd9000-7fed62cde000 rw-p 00000000 00:00 0 
7fed62cf4000-7fed62cf7000 rw-p 00000000 00:00 0 
7fff06836000-7fff06857000 rw-p 00000000 00:00 0                          [stack]
7fff06862000-7fff06863000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

【问题讨论】:

  • 你能给我们一个最小的完整例子吗? (我真的应该热键这个问题。)
  • 在我看来,您未能为 PriorityQueue 类定义复制构造函数。这很奇怪,因为您已经定义了一个赋值运算符。通常人们也不会为此烦恼。但是,您的赋值运算符有问题,并且您的复制构造函数不存在。由于您在 operator&lt;&lt; 函数中调用了复制构造函数,我敢打赌这就是错误所在。你应该阅读三法则,除非你遵循它,否则这门课永远不会开始工作。 stackoverflow.com/questions/4172722/what-is-the-rule-of-three
  • 顺便说一句,代码的正确性(或其他)关键取决于 List 类。除非他们能看到那个类,否则没有人能够诊断出问题。
  • 列表类是由我的课程讲师实现的,该文件是可编译和可实现的,没有错误。 (至少,我相信这是真的)
  • 三法则可能是问题所在。我会研究一下,谢谢你的链接

标签: c++ operator-overloading iostream coredump memory-mapping


【解决方案1】:

您的代码的最大问题是您模板化了 PriorityQueue 类,然后尝试在不同的位置使用创建 ostream 函数。

template <typename T>
class PriorityQueue : private List<T>
{
...
}

这个实现很好,但是当您创建 ostream 函数时,它无法将模板类型与您的类匹配。

template <typename T>
ostream& operator <<(ostream& os, const PriorityQueue<T>& rhs)
{
...
}

记住模板是在编译时创建的。当你创建一个 PriorityQueue 对象(让我们说 int 作为参数)然后你做了所有的 PriorityQueue::(insert function here) 实现。当您尝试调用 ostream ostream operator<<(ostream&, PriorityQueue<T>)。

要解决这个问题,您需要做的就是在类定义中创建 ostream 函数。

template <typename T>
class PriorityQueue : private List<T>
{
    ...
    friend ostream& operator <<(ostream& os, const PriorityQueue<T>& rhs)
    {
        ...
    }
}

现在每次创建 PriorityQueue 对象时,它都会有自己的 ostream 函数。所以PriorityQueue&lt;int&gt; 将有一个ostream&amp; operator &lt;&lt;(ostream&amp;, const PriorityQueue&lt;int&gt;&amp;) 函数,而PriorityQueue&lt;std::string&gt; 将有一个ostream&amp; operator &lt;&lt;(ostream&amp;, const PriorityQueue&lt;std::string&gt;&amp;) 函数。

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多