0

Attempting to overload a member function of a class, a redeclaration error occurred.

In class declarations even Qt reveal a problem:

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

This message is provided for the classes marked as "overload2" in the code below:

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;
...
}

The compilation fails complaining about overloading.

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;
          ^~~~~~~~~~~

The compilation works if one among "Overload 1/2" member function is deleted.

Why these functions can't be overloaded together?

Luca Jungla
  • 150
  • 8
  • 4
    you have exactly the same definitions but you just renamed the template parameter, endeing in twice the exact same thing. You need to tell us more about what is different between the 2 overloaded versions – amlucas Jun 26 '19 at 10:21
  • `IdsContainer` and `uniqueLabelContainer` seem to be same type - at least for this particular template instantiation. – Ajay Jun 26 '19 at 10:23
  • 1
    you have the same problem when you declare a `template foo(T t);` and a `template foo(S t);` – 463035818_is_not_an_ai Jun 26 '19 at 10:25
  • In a template point of view, it's exaclty like declare two functions `double f(double x);` and `double f(double y);`. To solve your problem, we have to knwo what you expect to do. – Caduchon Jun 26 '19 at 10:26
  • The names of a template's parameters are irrelevant - you have two declarations of `template bool readDataSet(const std::string&, A&, B&, C&) const;` – molbdnilo Jun 26 '19 at 10:29
  • I understand but these two template parameters are not the same. `IdsContainer` is a `std::vector` , `uniqueLabelContainer` is a `std::set`. Anyway, how can the compile know the type in compiling time? – Luca Jungla Jun 26 '19 at 10:29
  • 1
    @LucaJungla No, they're not - the types are parameters (with those names) for the templates. If you want to overload on specific types, do that. – molbdnilo Jun 26 '19 at 10:32
  • @molbdnilo Replacing the type parameter from `uniqueLabelContainer` to `std::set`, the problem disappear. I still confused in how the compiler check this issue. – Luca Jungla Jun 26 '19 at 10:49
  • 1
    I think you would be helped by reading some more about templates (and types) in [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 26 '19 at 11:02
  • Ok, then what you need is probably "template specialization". – Caduchon Jun 26 '19 at 11:09

0 Answers0