0

I've been working on these functions for the last two days and have my CPU and Wall times working finally after using boost,

The last thorn I just can't get me head around, I'm trying to pass a function with parameters into anther function that returns a value using std::bind,

I'm a student and this is all new to me, learning as I go,

int CA1::binarySearch(vector<int> v, int target)
{

    int top, bottom, middle;
    top = vecSize - 1;
    bottom = 0;

    while (bottom <= top)
    {
        middle = (top + bottom) / 2;
        if (v[middle] == target)
            return  middle;
        else if (v[middle] > target)
            top = middle - 1;
        else
            bottom = middle + 1;
    }
    return -1;
}


double CA1::measure(std::function<void()> function) {
    auto startCpu = boost::chrono::process_real_cpu_clock::now();
    auto startWall = boost::chrono::process_system_cpu_clock::now();

    function();

    auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
        (boost::chrono::process_real_cpu_clock::now() - startCpu);
    auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
        (boost::chrono::process_system_cpu_clock::now() - startWall);


    double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
    double wallTime = static_cast<double>(durationWall.count()) * 0.000001;

    /*return static_cast<double>(duration.count()) * 0.000001;*/

    cout << "Cpu time " << cpuTime << endl;
    cout << "Wall time " << wallTime << endl;

    return cpuTime;
}

void CA1::DoTests() {

    auto  time = measure(std::bind(binarySearch, vectorUnordered, 2));
}

The error I'm getting is:

error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member

But from what I've read and the code snippets I've seen by other users my code in DoTests() is correct.

1
  • 2
    since binarySearch is a non-static member function you need to qualify the name like &CA1::binarySearch, as well as bind an instance of CA1 for which the method will be invoked, e.g. std::bind(&CA1::binarySearch, this, vectorUnordered, 2) Commented Oct 23, 2014 at 19:35

2 Answers 2

2

Class functions have explicit 'this' parameter, which needs to passed in as first argument:

measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))
Sign up to request clarification or add additional context in comments.

Comments

2

Apparently, binarySearch is a member function of the class CA1 and member functions always have an implicit this parameter. When you use std::bind with a member function you will need to pass this as well (see "Using std::bind with member function, use object pointer or not for this argument?"), but that's not necessarily the best solution...

Does binarySearch need to be a member function? C++ doesn't force you to put everything inside a class and you shouldn't, unless it makes sense to do so. In general, a function that doesn't need access to the class' private members should not be a member function (see "How Non-Member Functions Improve Encapsulation"). Besides, the standard library already has std::binary_search.

12 Comments

Yes I need access to the private members of the class in binarySearch, I've changed my code, but now I'm getting a new error: see reference to function template instantiation 'std::function<void (void)>::function<std::_Bind<true,int,std::_Pmf_wrap<int (__thiscall CA1::* )(std::vector<int,std::allocator<_Ty>>,int),int,CA1,std::vector<_Ty,std::allocator<_Ty>>,int>,CA1 *const ,std::vector<_Ty,std::allocator<_Ty>> &,int>>(_Fx &&)' being compiled
I have to write the binarySearch for an assignment.
How does DoTests look like now?
void CA1::DoTests() { auto time = measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2)); }
That seems correct. Is this code similar to what you did?
|

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.