0

I have already looked at this question. This is the error message I receive:
E0304 no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=void (*)(), _Alloc=std::allocator<void (*)()>]" matches the argument list

My goal is to get all pointers to function in a vector so I can loop through them for various datasets by testing the different sorting algorithms My code:

    #include <iostream>
    #include <fstream>
    #include <chrono>
    using namespace std;
    #ifndef Sort_h
    #define Sort_h

    class Sort : public Algorithm
    {
    public:
        Sort() {
            algorithms.push_back((sortalgorithms::bubble));
            //algorithms.push_back(&(sortalgorithms::merge));
            //algorithms.push_back(&(sortalgorithms::insertion));
        }
        ~Sort();
        void load(string);
        void execute();
        void display();
        void stats();
        void select(int);
        void save();
        void configure();
    
    private:
        int type = 0;
        vector<int> data;
        string sortname;
        int vecSize = 0;
        string fileName;
        chrono::duration<double> time;
        std::vector<void (*)()> algorithms;
    };
    
    #endif
    #pragma once
    #include "Algorithm.h"
    #include <string>
    #ifndef algorithm_h
    #define algorithm_h
    using namespace std;
    class Algorithm {
    public:
        Algorithm() {};
        virtual ~Algorithm() {};
        virtual void load(string) = 0;
        virtual void execute() = 0;
        virtual void display() = 0;
        virtual void stats() = 0;
        virtual void select() = 0;
        virtual void save() = 0;
        virtual void configure() = 0;
    };
    
    #endif algorithm_h

I want to call a function from a vector of function pointers.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    using `using namespace std;` globally in a header file is a terrible idea. – kesarling He-Him Sep 07 '20 at 03:18
  • 1
    Also, please provide a minimal reproducible example, and please provide the `Algorithms` class – kesarling He-Him Sep 07 '20 at 03:19
  • 4
    Where and how is `sortalgorithms::bubble` declared? – Igor Tandetnik Sep 07 '20 at 03:27
  • We do need the information requested by others, especially since it seems rather odd for a sorting algorithm to take no arguments. – JaMiT Sep 07 '20 at 03:39
  • and why don't use `std::function`? – phuclv Sep 07 '20 at 04:31
  • Don't forget `sortalgorithms::bubble` (which is probably more important to this question than `class Algorithm`). Ideally, you should present a [mre] (MRE) that we can copy to reproduce the error. For a more concise example, see if you can drop derivation from `Algorithm` and drop the declarations of the class methods. – JaMiT Sep 07 '20 at 04:37

1 Answers1

0

It seems you have two errors here, 1) trying to add an invalid function reference and 2) trying to add a different pointer type

  1. When you pass a pointer as a parameter, that pointer must reference to an actual object in memory, in your case sortalgorithms::bubble is not a valid reference because it does not exists in memory yet, it is only a declaration. To solve this, you need to have an instance of sortalgorithms first and then pass the reference of bubble function, something like the following:
sortalgorithms algorithms;
algorithms.push_back(algorithms.bubble);

or

auto algorithms = new sortalgorithms();
algorithms.push_back(algorithms->bubble);
  1. Even when the signature of bubble is voidsortalgorithms::bubble(), this does not fit the required type void (*)() in the vector, so since you are using functions belonging to class sortalgorithms you must declare the vector as std::vector<void (sortalgorithms::*)()> algorithms

Putting all together:

class Sort
{
public:
    Sort()
    {
        algorithms.push_back(sortfunctions.bubble);
    }
    ...
private:
    ...
    std::vector<void (sortalgorithms::*)()> algorithms;
    sortalgorithms sortfunctions;
};

Edit

Although the original answer could apply, it does not includes the other possible scenario

  1. According the error message, sortalgorithms::bubble function has a signature other than expected by the vector algorithms, so, to fix the error you have to make sure all functions you pass as a parameter to algorithms.push_back() accomplish the expected signature

For example, if sortalgorithms::bubble would have the following signature:

void bubble(int a, int b)
{
    ...
}

Vector algorithms should be declared as:

std::vector<void (*)(int, int)> algorithms;

You can use any combination of the three cases, whatever applies to your case

Eddy
  • 473
  • 5
  • 9
  • 1) Going by the posted code, `sortalgorithms::bubble` does not even exist as a declaration. 2) It is fine to push a pointer to a function that has only a declaration in the current translation unit, as long as the function has a definition by link time. 3) How do you know it is possible to have an instance of `sortalgorithms`? As far as we know, `sortalgorithms` could be a namespace, not a class. 4) You do not need an instance of a class to get a pointer to a member function of that class. 5) A member function could have the type `void (*)()` if it is a static member function. – JaMiT Sep 07 '20 at 05:07
  • 1
    hmmm ok, 1) we should have the capacity to deduce some things with the provided information; 2) no, you are passing a pointer, unless ```sortalgorithms::bubble``` is a static function or ```sortalgorithms``` is a namespace, you cannot pass it as a pointer; 3) definitively ```sortalgorithms``` is not a namespace, otherwise he would not have the error; 4) remember, class is a type (does not exists in memory), object is an instance of the class, you can only have pointers to objects; 5) it is impossible that ```bubble``` is a static function, otherwise he would not have the error – Eddy Sep 07 '20 at 05:27
  • @JaMiT Actually I have to retract, I was doing poor assumptions, let me update my answer, thanks for your feedback – Eddy Sep 07 '20 at 09:34