我试图在我的ReceiveRequest中使用boost :: function在自己的线程上运行,但我必须发送错误的参数。 (至少那就是我认为编译器试图告诉我的)
以下是造成问题的行:
//some variables for the function call
std::string something("");
asio::system_error e();
asio::thread daThread();
CurrentRequest.payload;
//attempts to call function
CurrentRequest.Callback(something, &e, CurrentRequest.payload); //line 184
CurrentRequest.Callback(something, &e, &CurrentRequest.payload); //line 185
以下是编译器告诉我的内容:
g++ -o ss -pthread -lrt StringSocket.cpp main.cpp -I ~/asio/include -I ~/boost/include ~/boost/lib/*.a
StringSocket.cpp: In member function ‘void StringSocket::ProcessReceive()’:
StringSocket.cpp:184: error: no match for call to ‘(ReceiveRequest::receiveCallback) (std::string&, asio::system_error (*)(), void*&)’
/home/jsander/boost/include/boost/function/function_template.hpp:761: note: candidates are: R boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void, T0 = std::string*, T1 = asio::system_error&, T2 = void*]
StringSocket.cpp:185: error: no match for call to ‘(ReceiveRequest::receiveCallback) (std::string&, asio::system_error (*)(), void**)’
/home/jsander/boost/include/boost/function/function_template.hpp:761: note: candidates are: R boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void, T0 = std::string*, T1 = asio::system_error&, T2 = void*]
这是ReceiveRequest类:
class ReceiveRequest
{
typedef boost::function<void (std::string *message, asio::system_error& e, void *payload) > receiveCallback;
public:
receiveCallback Callback;
void* payload;
ReceiveRequest(receiveCallback _Callback, void* _payload)
{
Callback = _Callback;
payload = _payload;
}
~ReceiveRequest() { }
};
这些错误似乎区分了指针和对变量的引用。我认为它们可以作为参数互换使用。 boost :: function似乎也将我的所有局部变量都变成了引用。
我也很困惑我的一个参数传递为“e”变成“asio :: system_error(*)()”。为什么在我的变量中添加了第二对括号?
答案 0 :(得分:4)
这里有多个问题:
asio::system_error e();
这不是你想要的。由于C ++语法的工作方式,这实际上是声明函数 e
,它不接受任何参数并返回asio::system_error
。如果在括号中添加void
,则会更容易看到。它应该声明为:
asio::system_error e;
其次,您的typedef
表示您的功能应该引用system_error
:asio::system_error& e
。但是,当您传递上述内容时(假设您修复了第一个问题),您尝试传递指针:
CurrentRequest.Callback(..., &e, ....); // Should just be 'e'