Consider the follow code:
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.
Now I've tried two different types of implementation: 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);
}
Compiler:
||=== Build: Debug in TesterProgram (compiler: GNU GCC Compiler) ===|
E:\CODE\DSA\TesterProgram\BSTree.h||In member function 'elemType& BSTree::search(const elemType&) const [with elemType = Unit]':|
E:\CODE\DSA\TesterProgram\BSTree.h|189|warning: control reaches end of non-void function [-Wreturn-type]|
E:\CODE\DSA\TesterProgram\src\Regist.cpp||In member function 'Vector& Registration::GetHighestResults()':|
E:\CODE\DSA\TesterProgram\src\Regist.cpp|100|warning: reference to local variable 'vec' returned [-Wreturn-local-addr]|
E:\CODE\DSA\TesterProgram\src\Regist.cpp||In member function 'Vector& Registration::GetLowestResults()':|
E:\CODE\DSA\TesterProgram\src\Regist.cpp|125|warning: reference to local variable 'vec' returned [-Wreturn-local-addr]|
..\TesterProgram\vector.h||In member function 'const T& Vector::operator[](unsigned int) const [with T = Result]':|
..\TesterProgram\vector.h|277|warning: control reaches end of non-void function [-Wreturn-type]|
obj\Debug\main.o||In function main':|
E:\CODE\DSA\TesterProgram\main.cpp|81|undefined reference to
unsigned int Registration::GetCredits(std::_Placeholder<1>, std::reference_wrapper const>))(std::string, BSTree&)> >(std::_Bind(std::_Placeholder<1>, std::reference_wrapper const>))(std::string, BSTree&)>)'|
||=== Build failed: 1 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
What is going on here, I've had a look at related stuff on this site but nothing seems to directly answer this question.
I don't know how to use boost:: Is this something that is required, how would it be used?
Or is this a run time/ compiler issue? Is it not define in the compilation process when it needs to be? Is there a work around?
Thanks SO
Working on MVCE now.. update soon