宏#define函数的语法为“'{'令牌之前的预期表达式。”

时间:2019-01-18 02:23:35

标签: c++ c++11

我正在使用#define定义一组参数,以便稍后可以在main()中调用定义的名称。但是,我在第1行中出现了“ expected expression before '{' token”。我想知道我的语法是否错误。

#define ADV_1M_LEGACY_CONNECTABLE {                                     \
  .properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED, \
  .p_peer_addr = NULL,                                                  \
  .interval = APP_ADV_INTERVAL,                                         \
  .duration = APP_ADV_DURATION,                                         \
  .max_adv_evts = 0,                                                    \
  .channel_mask = 0,                                                    \
  .filter_policy = BLE_GAP_ADV_FP_ANY,                                  \
  .primary_phy = BLE_GAP_PHY_1MBPS,                                     \
  .secondary_phy = 0,                                                   \
  .set_id = 0,                                                          \
  .scan_req_notification = 0                                            \
}

来自评论:

  

我有一个调用函数:

m_adv_handle = advertising_init_common(&m_adv_data, ADV_1M_LEGACY_CONNECTABLE);
     

编辑代码后,在此调用行中,出现错误“ expected expression before ')' token”。

已更新:

 typedef struct
{
 ble_gap_adv_properties_t properties;              
 ble_gap_addr_t const    *p_peer_addr;             
 uint32_t                 interval;                
 uint16_t                 duration;                
 uint8_t                  max_adv_evts;            
 ble_gap_ch_mask_t        channel_mask;            
 uint8_t                  filter_policy;           
 uint8_t                  primary_phy;             
 uint8_t                  secondary_phy;          
 uint8_t                  set_id:4;                
 uint8_t                  scan_req_notification:1; 
 }ble_gap_adv_params_t; //******this being declare in another .h file

 #define ADV_1M_LEGACY_CONNECTABLE (ble_gap_adv_params_t) {               \
  .properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED, \
  .p_peer_addr = NULL,                                                  \
  .interval = APP_ADV_INTERVAL,                                         \
  .duration = APP_ADV_DURATION,                                         \
  .max_adv_evts = 0,                                                    \
  .channel_mask = 0,                                                    \
  .filter_policy = BLE_GAP_ADV_FP_ANY,                                  \
  .primary_phy = BLE_GAP_PHY_1MBPS,                                     \
  .secondary_phy = 0,                                                   \
  .set_id = 0,                                                          \
  .scan_req_notification = 0                                          \
 }

 static uint8_t advertising_init_common(ble_gap_adv_data_t* 
  ble_gap_adv_data, ble_gap_adv_params_t adv_params)
 {
  ret_code_t    err_code;
  ble_advdata_t advdata;
  ble_advdata_t srdata;
  uint8_t advHandle;

  ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};`

  // Build and set advertising data.
   memset(&ble_gap_adv_data->adv_data, 0, sizeof(ble_gap_adv_data->adv_data));

  advdata.name_type          = BLE_ADVDATA_FULL_NAME;
  advdata.include_appearance = true;
  advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

  memset(&srdata, 0, sizeof(srdata));
  srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
  srdata.uuids_complete.p_uuids  = adv_uuids;

  err_code = ble_advdata_encode(&advdata, ble_gap_adv_data->adv_data.p_data, &ble_gap_adv_data->adv_data.len);
APP_ERROR_CHECK(err_code);

err_code = ble_advdata_encode(&srdata, ble_gap_adv_data->scan_rsp_data.p_data, &ble_gap_adv_data->scan_rsp_data.len);
APP_ERROR_CHECK(err_code);


// Set advertising parameters.
err_code = sd_ble_gap_adv_set_configure(&advHandle, ble_gap_adv_data, 
&adv_params);
APP_ERROR_CHECK(err_code);
return advHandle;
}

int main()
{
 m_adv_handle = advertising_init_common(&m_adv_data, 
 ADV_1M_LEGACY_CONNECTABLE); //*********here i have "expected expression before ')' token message*******//
}

enter image description here

所以我想知道我又在哪里做错了。可以指导我吗?谢谢

2 个答案:

答案 0 :(得分:1)

您需要提供一种类型以及C99 compound literal

typedef struct conn_s { /*...*/ } conn_t;

#define ADV_1M_LEGACY_CONNECTABLE (conn_t){ /*...*/ }

int main() {
   conn_t conn = ADV_1M_LEGACY_CONNECTABLE;
}

这里是example

答案 1 :(得分:1)

这太大了,无法发表评论,因此是伪答案。

这是MCVE的近似值-存在一个重大问题,当我编译它时,看不到您看到的错误。

arm31.cpp

#include <cstdint>

typedef struct
{
    uint32_t    interval;
    uint16_t    duration;
    uint8_t     max_adv_evts;
    uint8_t     secondary_phy;
} ble_gap_adv_params_t;

typedef struct
{
    ble_gap_adv_params_t    adv_params;
    uint16_t                adv_program;
} ble_gap_adv_data_t;

#define ADV_1M_LEGACY_CONNECTABLE (ble_gap_adv_params_t) { \
        .interval = 29,                                    \
        .duration = 31,                                    \
        .max_adv_evts = 0,                                 \
        .secondary_phy = 0,                                \
}

static uint8_t advertising_init_common(ble_gap_adv_data_t *ble_gap_adv_data,
                                       ble_gap_adv_params_t adv_params)
{
    uint8_t advHandle = 19;
    ble_gap_adv_data->adv_params = adv_params;
    ble_gap_adv_data->adv_program = 31963;
    return advHandle;
}

#include <iostream>

int main()
{
    ble_gap_adv_data_t m_adv_data;
    uint8_t m_adv_handle = advertising_init_common(&m_adv_data,
                                                   ADV_1M_LEGACY_CONNECTABLE);
    std::cout
        << "handle:   " << m_adv_handle << ", "
        << "program:  " << m_adv_data.adv_program << ", "
        << "interval: " << m_adv_data.adv_params.interval << ", "
        << "duration: " << m_adv_data.adv_params.duration << ", "
        << "maxadvev: " << m_adv_data.adv_params.max_adv_evts << ", "
        << "secondph: " << m_adv_data.adv_params.secondary_phy << "\n";
}

编译

$ g++ -O3 -g -std=c++11 -Wall -Wextra -Werror -c arm31.cpp
$

不会产生任何警告(在使用自制G ++ 8.2.0的运行macOS 10.14.2 Mojave的Mac上)。要获取警告,我也需要使用-pedantic

$ g++ -O3 -g -std=c++11 -Wall -Wextra -Werror -pedantic -c arm31.cpp
arm31.cpp: In function ‘int main()’:
arm31.cpp:18:9: error: C++ designated initializers only available with -std=c++2a or -std=gnu++2a [-Werror=pedantic]
         .interval = 29,                                          \
         ^
arm31.cpp:39:52: note: in expansion of macro ‘ADV_1M_LEGACY_CONNECTABLE’
                                                    ADV_1M_LEGACY_CONNECTABLE);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
arm31.cpp:19:9: error: C++ designated initializers only available with -std=c++2a or -std=gnu++2a [-Werror=pedantic]
         .duration = 31,                                          \
         ^
arm31.cpp:39:52: note: in expansion of macro ‘ADV_1M_LEGACY_CONNECTABLE’
                                                    ADV_1M_LEGACY_CONNECTABLE);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
arm31.cpp:20:9: error: C++ designated initializers only available with -std=c++2a or -std=gnu++2a [-Werror=pedantic]
         .max_adv_evts = 0,                                       \
         ^
arm31.cpp:39:52: note: in expansion of macro ‘ADV_1M_LEGACY_CONNECTABLE’
                                                    ADV_1M_LEGACY_CONNECTABLE);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
arm31.cpp:21:9: error: C++ designated initializers only available with -std=c++2a or -std=gnu++2a [-Werror=pedantic]
         .secondary_phy = 0,                                      \
         ^
arm31.cpp:39:52: note: in expansion of macro ‘ADV_1M_LEGACY_CONNECTABLE’
                                                    ADV_1M_LEGACY_CONNECTABLE);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
arm31.cpp:22:1: error: ISO C++ forbids compound-literals [-Werror=pedantic]
 }
 ^
arm31.cpp:39:52: note: in expansion of macro ‘ADV_1M_LEGACY_CONNECTABLE’
                                                    ADV_1M_LEGACY_CONNECTABLE);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors

下一步

请获取此源代码并将其在您的系统上编译。然后修改它,直到它再现您所看到的错误。然后将修改后的代码发布为问题的编辑。