针对多个复制构造函数的编译器警告

Compiler warning for multiple copy constructors

本文关键字:编译器 警告 构造函数 复制      更新时间:2023-10-16

我想使用自定义类作为boost::heap::fibonacci_heap的类型,并且还能够迭代和修改堆的元素。我正在试验如何有序地遍历Boost提供的代码。堆优先级队列和更新给定的元素?.

我已经提出了一个工作的例子,但是我的Visual Studio 2010编译器警告我,我的类EdgeHeap有多个复制构造函数(警告文档)。

下面是警告(大致):

filepathboostheapfibonacci_heap.hpp(762): warning C4521: 'boost::heap::fibonacci_heap<T>': Multiple constructors
          with
          [
              T=EdgeHeap
          ]

我很困惑,因为我没有声明任何复制构造函数,所以唯一的应该是编译器自动添加的那些。多重构造函数从何而来?这也是我应该担心的吗?

#include <iostream>
#include <algorithm>
#include <boost/heap/fibonacci_heap.hpp>
class Edge
{
    public:
        int index;
        double weight;
        std::pair<int, int> vertices;
        Edge(int i, double w, int start, int end)
        {
            index = i;
            weight = w;
            vertices.first = start;
            vertices.second = end;
        }
};
class EdgeHeap
{
    typedef boost::heap::fibonacci_heap<EdgeHeap>::handle_type handle_t;
    public:
        handle_t handle;
        Edge data;
        EdgeHeap(const Edge &data_) : data(data_) {}
        bool operator<(EdgeHeap const & rhs) const
        {
            return data.weight < rhs.data.weight;
        }
};
void setup_handle(boost::heap::fibonacci_heap<EdgeHeap>::handle_type &&handle)
{
    (*handle).handle = handle;
}
int main()
{
    boost::heap::fibonacci_heap<EdgeHeap> heap;
    Edge e(0, 10, 0, 1);
    setup_handle(heap.push(e));
    Edge e1(1, 2, 1, 2);
    setup_handle(heap.push(e1));
    Edge e2(2, 80, 2, 0);
    setup_handle(heap.push(e2));
    std::find_if(heap.ordered_begin(), heap.ordered_end(),
    [&heap](const EdgeHeap &e) -> bool
    {
        if(e.data.index == 2)
        {
            const_cast<EdgeHeap &>(e).data.weight += 2;
            heap.increase(e.handle);
            return true;
        }
        return false;
    });
    std::for_each(heap.ordered_begin(), heap.ordered_end(),
    [](const EdgeHeap &e)
    {
        std::cout << e.data.weight << std::endl;
    });
}

看头文件,boost::heap::fibonacci_heap确实有多个复制构造函数:

fibonacci_heap(fibonacci_heap const &);
fibonacci_heap(fibonacci_heap &);

(编译器消息不是关于EdgeHeap或您的类型之一)

根据VS文档,这是一个信息警告,在这种情况下不用担心。