1

如果我注释掉所有的 boost 部分,为什么这段代码会起作用,但是当我包含它们时,调试器似乎无法识别与蓝牙相关的代码?

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <boost/asio.hpp>
using namespace::boost::asio;
// These two require ws2_32.lib
#include <Winsock2.h>
#include <Ws2bth.h>
// This one requires Bthprops.lib
#include <BluetoothAPIs.h>
using namespace std;



BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)};
BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
    sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
    1,
    0,
    1,
    1,
    1,
    15,
    NULL };
BLUETOOTH_DEVICE_INFO m_device_info = { sizeof(BLUETOOTH_DEVICE_INFO), 0, };

// Note:
// Radio - is the thing plugged in/attached to the local machine.
// Device - is the thing that is connected to via the Bluetooth connection.


int main()
{
    HANDLE m_radio = NULL;
    HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
    int m_device_id;

    serial_port_base::baud_rate BAUD(9600);
    serial_port_base::character_size CSIZE( 8 );
    serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
    serial_port_base::parity PARITY( serial_port_base::parity::none );
    serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );

    io_service io;
    serial_port port( io, "COM3" );
    port.set_option( BAUD );
    port.set_option( CSIZE );
    port.set_option( FLOW );
    port.set_option( PARITY );
    port.set_option( STOP );

    unsigned char command[1] = {0};
    bool change = false;
    bool checker;

    while( true ) {
    do
    {
        ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
        m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);

// Next for every radio, get the device
        m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
        system("cls");
        if(m_bt_dev != NULL)
            cout << "\nBluetoothFindFirstDevice() is working!\n";
        else
            cout << "\nBluetoothFindFirstDevice() failed with error code " << GetLastError() << endl;

        m_device_id = 0;
        checker = m_device_info.fConnected;

// Get the device info
        do
        {
            char deviceName[248];
            char deviceName2[248] = "SCH-R920";
            for( int i = 0; i < 248; i++ )
            {
                deviceName[i] = (char) m_device_info.szName[i];
            }

            if( !strcmp( deviceName, deviceName2 ) )
            {
            cout << "\nDevice " << m_device_id << ":";
            wcout << "\n\tInstance Name: " << m_device_info.szName;
            cout << "\n\tAddress: " << hex << uppercase << static_cast<unsigned>(m_device_info.Address.rgBytes[5]) << ":" <<
            static_cast<unsigned>(m_device_info.Address.rgBytes[4]) << ":" << static_cast<unsigned>(m_device_info.Address.rgBytes[3]) << ":" <<
            static_cast<unsigned>(m_device_info.Address.rgBytes[2]) << ":" << static_cast<unsigned>(m_device_info.Address.rgBytes[1]) << ":" <<
            static_cast<unsigned>(m_device_info.Address.rgBytes[0]);                                // need to learn to do templates
            cout << "\n\tClass: 0x00" << nouppercase << m_device_info.ulClassofDevice; // template for the two 0's in quotes
            cout << "\n\tConnected: " << ( m_device_info.fConnected != 0 ? "True" : "False" );
            cout << "\n\tAuthenticated: " << ( m_device_info.fAuthenticated != 0 ? "True" : "False" );
            cout << "\n\tRemembered: " << ( m_device_info.fRemembered != 0 ? "True" : "False" );
            }
            else
                m_device_id++;

        } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));

        if( m_device_info.fConnected == checker )
        {
            change = true;
        }

// NO more device, close the device handle
        if(BluetoothFindDeviceClose(m_bt_dev) == TRUE)
            wcout << "\nBluetoothFindDeviceClose(m_bt_dev) is OK!\n";
        else
            cout << "\nBluetoothFindDeviceClose(m_bt_dev) failed with error code " << GetLastError() << endl;
    } while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));

    if( change )
    {
        if( m_device_info.fConnected != 0 )
        {
            cout << "It's connected";
            cin.get();
            //write( port, buffer( '1', 1 ) );
        }
        else
        {
            cout << "It's not connected";
            cin.get();
            //write( port, buffer( '0', 1 ) );
        }
    }

    change = false;

    }

    std::cout << "\n\n_____________________________\n"
         << "Press any key to exit....";
    std::cin.get();
    return 0;
}

调试器给了我:

1>------ Build started: Project: Winsock, Configuration: Debug Win32 ------

1>  Source.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:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(20): error 
C2146: syntax error : missing ';' before identifier 'm_bt_find_radio'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(21): error C2146: syntax error : missing ';' before identifier 'm_search_params'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(23): error C2078: too many initializers

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C2146: syntax error : missing ';' before identifier 'm_device_info'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C2078: too many initializers

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2065: 'HBLUETOOTH_DEVICE_FIND' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2146: syntax error : missing ';' before identifier 'm_bt_dev'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2065: 'm_bt_dev' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(65): error C2228: left of '.dwSize' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(68): error C2065: 'm_bt_dev' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(68): error C3861: 'BluetoothFindFirstDevice': identifier not found

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(70): error C2065: 'm_bt_dev' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(76): error C2228: left of '.fConnected' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(85): error C2228: left of '.szName' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(91): error C2228: left of '.szName' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(92): error C2228: left of '.Address' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(92): error C2228: left of '.rgBytes' must have class/struct/union

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(93): error C2228: left of '.Address' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(93): error C2228: left of '.rgBytes' must have class/struct/union

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(94): error C2228: left of '.Address' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(94): error C2228: left of '.rgBytes' must have class/struct/union

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(95): error C2228: left of '.Address' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(95): error C2228: left of '.rgBytes' must have class/struct/union

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(96): error C2228: left of '.ulClassofDevice' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(97): error C2228: left of '.fConnected' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(98): error C2228: left of '.fAuthenticated' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(99): error C2228: left of '.fRemembered' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(104): error C2065: 'm_bt_dev' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(104): error C3861: 'BluetoothFindNextDevice': identifier not found

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(106): error C2228: left of '.fConnected' must have class/struct/union

1>          type is 'int'

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(112): error C2065: 'm_bt_dev' : undeclared identifier

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(112): error C3861: 'BluetoothFindDeviceClose': identifier not found

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(116): error C3861: 'BluetoothFindNextRadio': identifier not found

1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(120): error C2228: left of '.fConnected' must have class/struct/union

1>          type is 'int'

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

3 回答 3

0

尝试在所有“#include”行之后推送所有“使用命名空间”行

于 2012-11-26T18:33:11.910 回答
0

胡乱猜测:

使用 asio,您可以拉入 Windows 头文件(套接字需要),其中定义了与 STL 最小/最大冲突的讨厌的最小最大宏。您必须在包含 boost asio 之前定义 NOMINMAX。

于 2012-12-08T20:19:04.923 回答
0

最好包含 c 而不是 c++,然后是第 3 方,然后按顺序包含您自己的标头(cc 文件除外,首先包含您自己的声明标头)。在这种情况下,可能有一个定义是 asio 正在检查是否存在干扰。winsock 的东西是一个检查开始的好地方。我想你会发现这可能是问题(或非常接近)Boost::asio winsock 和 winsock 2 兼容性问题

这些问题在包含 windows.h 的标头中很常见。

于 2012-11-26T19:11:31.697 回答