askatral
askatral

Reputation: 123

error C2064 for using threads in unit testing

//-------------cpp11 BlockingQueue.h
//------test2()----------enques messages
template <typename T>
int BlockingQueue<T>::test2()
{
    //BlockingQueue<std::string> q;
    std::thread t(&BlockingQueue<T>::test);


    for (int i = 0; i<15; ++i)
    {
    std::ostringstream temp;
    temp << i;
    std::string msg = std::string("msg#") + temp.str();
    {
        std::lock_guard<std::mutex> l(mtx_);
    std::cout << "\n   main enQing " << msg.c_str();
    }
    enQ(msg);
    std::this_thread::sleep_for(std::chrono::milliseconds(3));
    }
    enQ("quit");
    std::cout << "\n   _q size is  " << q_.size() << std::endl;
    t.join();
    //test();
    return q_.size();
}

//--------<test()>----------- dequeues messages

template <typename T>
void BlockingQueue<T>::test()
{
std::string msg;
do
{
msg = deQ();
{
std::lock_guard<std::mutex> l(mtx_);
std::cout << "\n  thread deQed " << msg.c_str();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (msg != "quit");
}
//----------<unit test project------------>
#include"gtest\gtest.h"
#include"Cpp11-BlockingQueue.h"

TEST(testBQ, simpleTest)
{
    BlockingQueue<std::string> obj;
    EXPECT_EQ(0, obj.test2());
}

Error: Error 1 error C2064: term does not evaluate to a function taking 0 arguments in unitTest_MyBQ

I am trying to test my blocking queue implementation using google test. When I don't use thread in test2() method to invoke test() method, unitTest runs fine, but throws the above error when thread is used to invoke test().

How do I do unit testing for threads?

Upvotes: 0

Views: 76

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52601

Make it std::thread t(&BlockingQueue<T>::test, this);

A pointer-to-member-function is never a callable taking 0 arguments - it needs at least a pointer or reference to the object that the member function is to be called on (plus the parameters in its signature).

Upvotes: 1

Related Questions