函数模板问题

时间:2015-06-08 10:06:12

标签: c++ templates binding functional-programming

请考虑以下代码:

Unit& accessTree(std::string unitId2Find,  BSTree<Unit> &tree)
{
    Unit searchU;
    searchU.SetId(unitId2Find);
    return tree.search(searchU);
}

int main()
{
    using namespace std::placeholders;
    BSTree<Unit> tesTree;
    auto getUnit = std::bind(accessTree, _1, std::cref(tesTree));'

    Registration R;

..... //Setting required data

int i = R.GetCredits(getUnit); //Causes the error.

现在我尝试了两种不同类型的实现: 1

template<typename Fn>
unsigned Registration::GetCredits(Fn access)
{
    unsigned sum = 0;
    for(unsigned i = 0; i < GetSize(); i++)
        sum += results[i].GetCredits(access(results[i].GetUnit()));

    return sum;
}

template<typename Fn>
float Registration::CalculateGPA(Fn access)
{
    float total = 0;

    for (int i = 0; i < results.getLength(); i++)
    {
        total = total + results[i].GetGPAvalue() * results[i].GetCredits(access(results[i].GetUnit()));
    }

    return total / GetCredits(access);
}

2

unsigned Registration::GetCredits(Unit& (*access) (std::string & uId))
{
    unsigned sum = 0;
    for(unsigned i = 0; i < GetSize(); i++)
        sum += results[i].GetCredits(*access(results[i].GetUnit()));

    return sum;
}

float Registration::CalculateGPA(Unit& (*access) (std::string & uId))
{
    float total = 0;

    for (int i = 0; i < results.getLength(); i++)
    {
        total = total + results[i].GetGPAvalue() * results[i].GetCredits(*access(results[i].GetUnit()));
    }

    return total / GetCredits(func);
}

编译器: || === Build:在TesterProgram中调试(编译器:GNU GCC编译器)=== | E:\ CODE \ DSA \ TesterProgram \ BSTree.h ||在成员函数&#39; elemType&amp; BSTree :: search(const elemType&amp;)const [with elemType = Unit]&#39;: E:\ CODE \ DSA \ TesterProgram \ BSTree.h | 189 |警告:控制到达非空函数的末尾[-Wreturn-type] | E:\ CODE \ DSA \ TesterProgram \ src \ Regist.cpp ||在成员函数&#39; Vector&amp;注册:: GetHighestResults()&#39;:| E:\ CODE \ DSA \ TesterProgram \ src \ Regist.cpp | 100 |警告:引用局部变量&#39; vec&#39;返回[-Wreturn-local-addr] | E:\ CODE \ DSA \ TesterProgram \ src \ Regist.cpp ||在成员函数&#39; Vector&amp;注册:: GetLowestResults()&#39;:| E:\ CODE \ DSA \ TesterProgram \ src \ Regist.cpp | 125 |警告:引用局部变量&#39; vec&#39;返回[-Wreturn-local-addr] | .. \ TesterProgram \ vector.h ||在成员函数&#39; const T&amp; Vector :: operator [](unsigned int)const [with T = Result]&#39;: .. \ TesterProgram \ vector.h | 277 |警告:控制到达非空函数的末尾[-Wreturn-type] | obj \ Debug \ main.o ||函数main':| E:\CODE\DSA\TesterProgram\main.cpp|81|undefined reference to unsigned int Registration :: GetCredits(std :: _ Placeholder&lt; 1&gt ;,std :: reference_wrapper const&gt;))(std :: string,BSTree&amp;)&gt; &gt;(std :: _ Bind(std :: _ Placeholder&lt; 1&gt ;,std :: reference_wrapper const&gt;))(std :: string,BSTree&amp;)&gt;)&#39; | || ===构建失败:1个错误,4个警告(0分钟,0秒(秒))=== |

这里发生了什么,我已经看过这个网站上的相关内容,但似乎没有什么能直接回答这个问题。

我不知道如何使用boost ::这是必需的,它将如何使用?

或者这是运行时/编译器问题?是否需要在编译过程中定义?有工作吗?

非常感谢

立即开始使用MVCE ..很快就会更新

0 个答案:

没有答案