0

试图重载类的成员函数,发生重新声明错误。

在类声明中,甚至 Qt 也揭示了一个问题:

error: class member cannot be redeclared
note: previous declaration is here

此消息是为以下代码中标记为“overload2”的类提供的:

template <class GraphReaderType, class GraphType>
class GraphsDataset {

public:

    template <typename BoostGraphsContainer, typename LabelsContainer>
    bool readDataset(const std::string& datasetPath, BoostGraphsContainer& graphsContainer, LabelsContainer& labels) const;

    // "Overload 1"
    template <typename BoostGraphsContainer, typename LabelsContainer, typename IdsContainer>
    bool readDataset(const std::string& datasetPath, BoostGraphsContainer& graphsContainer, LabelsContainer& labels,
             IdsContainer& ids) const;
    //"Overload 2"
    template <typename BoostGraphsContainer, typename LabelsContainer, typename uniqueLabelContainer>
    bool readDataset(const std::string& datasetPath, BoostGraphsContainer& graphsContainer, LabelsContainer& labels, uniqueLabelContainer& labelsSet) const;
...
}

编译失败,抱怨重载。

GraphsDataset.hpp:120: error: ‘template<class GraphReaderType, class GraphType> template<class BoostGraphsContainer, class LabelsContainer, class uniqueLabelContainer> bool spare::GraphsDataset<GraphReaderType, GraphType>::readDataset(const string&, BoostGraphsContainer&, LabelsContainer&, uniqueLabelContainer&) const’ cannot be overloaded
     bool readDataset(const std::string& datasetPath, BoostGraphsContainer& graphsContainer, LabelsContainer& labels, uniqueLabelContainer& labelsSet) const;
          ^~~~~~~~~~~

如果删除“Overload 1/2”成员函数之一,则编译工作。

为什么这些函数不能一起重载?

4

0 回答 0