使用boost asio时出错

时间:2018-02-06 07:46:24

标签: c++ boost boost-asio

我正在使用我已下载的预构建1.66(lib32-msvc-14.0)。我正在使用Visual Studio 2015。 我可以毫无问题地运行样本。 但是在我的项目中,只要我包含它(一个simle包装器),就会产生很多错误。

这是我的简单包装器:

Net.h:

#ifndef _PCNET_h
#define _PCNET_h

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>
#include <string>

using boost::asio::ip::tcp;
typedef unsigned char byte;
typedef void(*newDataCallbackPtr)(byte cmd, byte *data, int len);

namespace net {

    void init(std::string host, std::string port);

    void send(byte cmd, byte *data, int len);

    void registerCB(newDataCallbackPtr cb);

    void join();
}

#endif

Net.cpp:

#include "Net.h"

const int max_length = 66000;
tcp::socket *s;
std::string delim = ";z|";
std::string recvData;
std::thread *t, *t2;
newDataCallbackPtr CB;

void readThrd();
//Public
void net::init(std::string host, std::string port) {
    boost::asio::io_context io_context;

    s = new tcp::socket(io_context);
    tcp::resolver resolver(io_context);
    boost::asio::connect(*s, resolver.resolve(host, port));

    t = new std::thread([&io_context]() { io_context.run(); });
    t2 = new std::thread(readThrd);

}

void net::send(byte cmd, byte *data, int len) {
    byte *buff = new byte[len + 4];
    buff[0] = cmd;
    memcpy(buff + 1, data, len);
    buff[len + 1] = ';';
    buff[len + 2] = 'z';
    buff[len + 3] = '|';
    boost::asio::write(*s, boost::asio::buffer(buff, len + 4));
    delete[] buff;
}

void net::registerCB(newDataCallbackPtr cb) {
    CB = cb;
}

void net::join() {
    t->join();
}
//Prv8

void readThrd() {
    while (true)
    {
        size_t n = boost::asio::read_until(*s, boost::asio::dynamic_buffer(recvData), delim);

        byte *buff = new byte[n - 3];
        memcpy(buff, recvData.c_str(), n - 3);
        recvData.erase(0, n);
        CB(buff[0], buff + 1, (int)n - 4);
    }
}

我的项目的其余部分编译没有任何问题。但是一旦我包含这个Net.h(在另一个.cpp中),编译器在winuser和asio头文件中给了我很多错误:

1>------ Build started: Project: pcMode, Configuration: Debug Win32 ------
1>  PCutil.cpp
1>  Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>  - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>  - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>  Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): warning C4091: 'typedef ': ignored on left of 'tagINPUT' when no variable is declared
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): error C2143: syntax error: missing ';' before 'constant'
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): error C2059: syntax error: 'constant'
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5843): error C2061: syntax error: identifier 'LPINPUT'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(74): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(74): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(75): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2976: 'boost::asio::detail::executor_binder_result_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(82): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(82): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(83): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2976: 'boost::asio::detail::executor_binder_result_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(90): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(90): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(91): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2974: 'boost::asio::detail::executor_binder_result_type': invalid template argument for 'T', type expected
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(98): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(98): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(99): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2974: 'boost::asio::detail::executor_binder_result_type': invalid template argument for 'T', type expected
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): warning C4544: '<unnamed-symbol>': default template argument ignored on this template declaration
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(108): note: see declaration of '<unnamed-symbol>'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(118): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(118): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(119): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(124): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(124): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(125): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): warning C4544: '<unnamed-symbol>': default template argument ignored on this template declaration
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(133): note: see declaration of '<unnamed-symbol>'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(144): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(144): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(145): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(151): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(151): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(152): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(219): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(493): note: see reference to class template instantiation 'boost::asio::executor_binder<T,Executor>' being compiled
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(219): error C2955: 'boost::asio::detail::executor_binder_argument_type': use of class template requires template argument list
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(220): error C2976: 'boost::asio::detail::executor_binder_argument_types': too few template arguments
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): note: see declaration of 'boost::asio::detail::executor_binder_argument_types'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(220): error C2955: 'boost::asio::detail::executor_binder_argument_types': use of class template requires template argument list
1>  c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): note: see declaration of 'boost::asio::detail::executor_binder_argument_types'
1>  Net.cpp
1>  Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>  - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>  - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>  Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

至于PCutil。它有很多代码,我删除了不相关的部分。这是相关部分: PCutil.h:

#ifndef _PCUTIL_h
#define _PCUTIL_h

#include <iostream>
#include <chrono>
#include <thread>
#include <queue>
#include <stdarg.h>
#include "pinNameDef.h"

extern int ardDigitalPin2PinNum[];


#define HIGH 1
#define OUTPUT 1
#define LOW 0
#define INPUT 0

typedef unsigned char byte;


extern byte exE2PR[];
extern byte inE2PR[];

void PCSend(NETCMDS id, int len, byte b1 = 0, byte b2 = 0, byte b3 = 0, byte b4 = 0, byte b5 = 0, byte b6 = 0, byte b7 = 0, byte b8 = 0);
void PCSend(NETCMDS id, int len, byte *data);


void init(char* host, char* port);
void joinNet();

#endif

PCutil.cpp

#include "PCutil.h"
#include "pinNameDef.h"
#include "../spec.h"
#include "../config.h"
#include "Net.h"

byte exE2PR[PCE2PRSIZE];
byte inE2PR[PCE2PRSIZE];

void PCSend(NETCMDS id, int len, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) {
    byte b[] = { b1,b2,b3,b4,b5,b6,b7,b8 };
    net::send(id, b, len + 1);
}
void PCSend(NETCMDS id, int len, byte *data) {
    net::send(id, data, len);
}


void netCB(byte cmd, byte *data, int len) {
    //some codes
}

void init(char* host, char* port) {
    Net init
    net::registerCB(netCB);
    net::init(host, port);
}
void joinNet() {
    net::join();
}

这里有什么想法吗? 感谢。

0 个答案:

没有答案