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.

JTK
  • 1,469
  • 2
  • 22
  • 39
  • 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)` – Piotr Skotnicki Oct 23 '14 at 19:35

2 Answers2

2

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

measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2))
theamk
  • 1,420
  • 7
  • 14
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.

Community
  • 1
  • 1
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • 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::function>,int),int,CA1,std::vector<_Ty,std::allocator<_Ty>>,int>,CA1 *const ,std::vector<_Ty,std::allocator<_Ty>> &,int>>(_Fx &&)' being compiled – JTK Oct 23 '14 at 19:49
  • I have to write the binarySearch for an assignment. – JTK Oct 23 '14 at 19:52
  • How does `DoTests` look like now? – Paul Manta Oct 23 '14 at 19:54
  • void CA1::DoTests() { auto time = measure(std::bind(&CA1::binarySearch, this, vectorUnordered, 2)); } – JTK Oct 23 '14 at 19:55
  • That seems correct. Is [this code](http://ideone.com/LDuynC) similar to what you did? – Paul Manta Oct 23 '14 at 20:05
  • Yeah pretty much, calling DoTest() in main and returning -1 in binarySearch() – JTK Oct 23 '14 at 20:08
  • Those aren't differences that would cause compilation errors. The mistake has to be somewhere else. In case your compiler doesn't enable C++11 by default, did you enable it yourself? If unsure, try to compile the code that I gave you see what happens. – Paul Manta Oct 23 '14 at 20:13
  • @Johntk That's a whole lot of code. Try to reduce it to the bare minimum to make it easier for me and others to help you. – Paul Manta Oct 23 '14 at 20:14
  • Sorry my bad, I tried to compile your code and I got the same error I was getting with mine, so C++11 is not enabled presumably? I'm using visual studio 2013 – JTK Oct 23 '14 at 20:16
  • Most likely. What compiler do you use? If you use `g++` or `clang`, add the option `-std=c++11`. – Paul Manta Oct 23 '14 at 20:19
  • Is C++11 not enabled on VS 2013, I've checked Google but can't get a definitive answer. – JTK Oct 23 '14 at 20:26
  • Visual Studio has C++11 enabled by default. – Paul Manta Oct 23 '14 at 20:34