Access into a Binary Search Tree via a bound function in a function template

50 views Asked by At

Consider the following.

class ClassA
{
    public:

        template <typename Fn>
        int getTotal(Fn access);

        void setId(std::string id, int index);

    private:
        std::string idVec[3];
};

template <typename Fn>
int ClassA::getTotal(Fn access)
{
    int total = 0;

    for (int i = 0; i < 3; i++)
    {
        total += access(idVec[i]).getValue();
    }

    return total;
}

void ClassA::setId(std::string id, int index)
{
    idVec[index] = id;
}

class Unit
{
    public:
        Unit();

        int getValue() const;
        std::string GetId() const;

        void setValue(int i);
        void setId(std::string s);

    private:
        std::string id;
        int value;
};
Unit::Unit()
{
    value = 0;
    id = "Not Set";
}

int Unit::getValue() const
{
    return value;
}

std::string Unit::GetId() const
{
    return id;
}

void Unit::setValue(int i)
{
    value = i;
}

void Unit::setId(std::string s)
{
    id = s;
}

Unit& accessTree(std::string id, BSTree<Unit> &tree)
{
    Unit tempU;
    tempU.setId(id);
    return tree.search(tempU);
}

int main()
{
    BSTree<Unit> tree;
    auto getUnit = std::bind(accessTree, _1, std::ref(tree));

    ClassA A;
    Unit U1, U2, U3;

    U1.setValue(1);
    U2.setValue(2);
    U3.setValue(3);
    U1.setId("AAA111");
    U2.setId("BBB222");
    U3.setId("CCC333");

    A.setId(U1.GetId(), 0);
    A.setId(U2.GetId(), 1);
    A.setId(U3.GetId(), 2);

    tree.insert(U2);
    tree.insert(U1);
    tree.insert(U3);

    int test = A.getTotal(getUnit); //LINE 50

    std::cout << test << std::endl;

    return 0;
}

Link to my BSTree Code:

http://pastebin.com/eqWtUDuK

Error:

||=== Build: Debug in testMap (compiler: GNU GCC Compiler) ===|

include\BSTree.h||In member function 'elemType& BSTree<elemType>::search(const elemType&) const [with elemType = Unit]':|

include\BSTree.h|189|warning: control reaches end of non-void function [-Wreturn-type]|

obj\Debug\main.o||In function `main':|

E:\CODE\CGPP\Lab8\testMap\main.cpp|50|undefined reference to `int ClassA::getTotal<std::_Bind<Unit& (*(std::_Placeholder<1>, std::reference_wrapper<BSTree<Unit> >))(std::string, BSTree<Unit>&)> >(std::_Bind<Unit& (*(std::_Placeholder<1>, std::reference_wrapper<BSTree<Unit> >))(std::string, BSTree<Unit>&)>)'|

||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

So this is the MCVE. Not 100% sure what I've done wrong.

What have I missed?

My assumption is that is has something to do with std::bind being defined at run time(From my understand), which is too late so to speak for my programs needs.

Thanks for any and all help, been working on this for a few days now.

0

There are 0 answers