【问题标题】:Visual Studio C ++ Reading HeadersVisual Studio C++ 阅读标题
【发布时间】:2012-11-26 18:30:41
【问题描述】:

为什么如果我注释掉所有的 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 ==========

【问题讨论】:

  • 只是猜测。看起来编译器错误是由于 _WIN32_WINNT 未定义或未正确定义。我会在项目设置中为此添加一个定义,然后将#include &lt;winsock2.h&gt; 移到最开始,然后再包含其他任何内容。在 x64 构建下,如果未定义 WIN64,您也可能会遇到问题,因此您还需要为此添加项目设置定义(仅适用于 x64 构建)。

标签: c++ visual-studio boost-asio


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 有些人会认为最好反过来做:stackoverflow.com/questions/2762568/…
    • 是的,我想这是有争议的,我认为隐藏的依赖关系虽然可能很糟糕。我认为包含你使用的范例是最安全的。谷歌风格指南对其中一些google-styleguide.googlecode.com/svn/trunk/… 有相反的论点,我认为最好不要隐藏要求,尽管这可能是个人喜好。 stackoverflow 问题似乎是一个很好的观点,但只要它编译参数就不是:-)
    • 好的,我将 boost::asio 移到了蓝牙之后,我再也没有收到这些错误了。但现在它抛出了一个异常:test.exe 中 0x760DC41F 处的未处理异常:Microsoft C++ 异常:boost::exception_detail::clone_impl<:exception_detail::error_info_injector>> 在内存位置 0x0040F068 .
    • 我认为您可能需要检查蓝牙头是否也有一些 windows.h 要求。除了遍历这些标头以查找它们尝试包含的内容之外,可能没有其他办法。我很抱歉,但很难知道该标题包含什么。可能是您实际上确实需要按照编译器的说法定义 Windows 版本。这将取决于您使用的 Windows 版本,也许这会有所帮助msdn.microsoft.com/en-gb/library/windows/desktop/…
    【解决方案3】:

    猜测:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多