0

I'm creating a system that has 7 categories of data (thus, 7 different objects) that users can add, edit and delete from. To be more efficient, I made a utility class extending from that defines add, edit and delete functions more specific to the program. Because the user can input data in the Add function, I'm passing a class member function pointer into List's addObject class that inputs the data specific for that object. Thus, the List class adds the object and each class has a class member function for their specific variables.

I have this error in the addObject class:

Called object type 'bool (Product::*)()' is not a function or function pointer

it seems like the List class can't access the Product data. When I added #include "Product.h" in the head, the compiler sent many errors about double initialization of Product functions, which is very odd because ALL classes have #ifndef, #define and #endif at top and bottom respectfully.

Here's my code - I added what I think is important for understanding, so let me know if you need more to understand:

Main:

#include <list>
#include <iterator>
#include <algorithm>
#include "Product.h"
#include "List.h"
int main () 
{
    bool (Product::*prodFunc) () = &Product::addProduct;
    allProducts.addObject(allProducts.back(), prodFunc); 
}

in the List Class:

template<class DT>
bool List<DT>::addObject(DT & previous, bool (DT::* func)())
{
    bool success = true;
    int newID = 1 + (previous.getID());
    cout << "New ID: " << newID << endl;
    previous.setID(newID);
    func(); //Error here
    return success;
}

Product's addProduct class:

bool Product::addProduct()
{
    bool success = true;
    cout << "Please enter the following for your new product:\n";
    addItem();
    return success;
}

I'm sure (or at least hope) it's something real simple that I'm missing. Thanks!

1
  • 4
    func(); //Error here -- Hint -- When you call a member function by pointer, you need an object specified somewhere in the call. Where is the object in that call? Commented Apr 24, 2018 at 1:12

2 Answers 2

2

Assuming you want to call the member function on the previous parameter, the way you make such a call is like this:

(previous.*func)();

Or, because the syntax looks gnarly, most advise using std::invoke instead

std::invoke(func, previous);

Which will automatically deduce that func is a member function and should be called with previous

Sign up to request clarification or add additional context in comments.

Comments

0

You can not pass a pointer to a non-static member function as a bare function; it must be called on some object of the class's type. The easy way to implement this is with std::bind.

auto prodFunc = std::bind(&Product::addProduct, &allProducts.back());
allProducts.addObject(allProducts.back(), &prodFunc);

Alternatively, you could declare Product::addProduct() as a static member function, in which case your code should function as intended. Looking at how you are using it, that may be what you actually want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.