【问题标题】:C++ error: no matching function for call to ''"C++ 错误:没有匹配的函数调用“”
【发布时间】:2022-01-05 12:35:37
【问题描述】:

虽然这个问题之前已经在社区提出过,但是其他的案例和我的不一样,他们的解决方案不能应用到我的案例中。

所以我有一个非常大的标题“rrc_nbiot.h”文件,其结构如下:

#include "rrc.h"

namespace asn1 {
namespace rrc {
...
// SystemInformationBlockType2-NB-r13 ::= SEQUENCE
struct sib_type2_nb_r13_s {
  struct freq_info_r13_s_ {
    bool                  ul_carrier_freq_r13_present = false;
    carrier_freq_nb_r13_s ul_carrier_freq_r13;
    uint8_t               add_spec_emission_r13 = 1;
  };
  using multi_band_info_list_r13_l_ = bounded_array<uint8_t, 8>;
  struct freq_info_v1530_s_ {
    tdd_ul_dl_align_offset_nb_r15_e tdd_ul_dl_align_offset_r15;
  };

  // member variables
  bool                          ext                              = false;
  bool                          multi_band_info_list_r13_present = false;
  bool                          late_non_crit_ext_present        = false;
  rr_cfg_common_sib_nb_r13_s    rr_cfg_common_r13;
  ue_timers_and_consts_nb_r13_s ue_timers_and_consts_r13;
  freq_info_r13_s_              freq_info_r13;
  time_align_timer_e            time_align_timer_common_r13;
  multi_band_info_list_r13_l_   multi_band_info_list_r13;
  dyn_octstring                 late_non_crit_ext;
  // ...
  // group 0
  bool cp_reest_r14_present = false;
  // group 1
  bool serving_cell_meas_info_r14_present = false;
  bool cqi_report_r14_present             = false;
  // group 2
  bool                         enhanced_phr_r15_present = false;
  bool                         cp_edt_r15_present       = false;
  bool                         up_edt_r15_present       = false;
  copy_ptr<freq_info_v1530_s_> freq_info_v1530;

  // sequence methods
  SRSASN_CODE pack(bit_ref& bref) const;
  SRSASN_CODE unpack(cbit_ref& bref);
  void        to_json(json_writer& j) const;
};
...
} // namespace rrc
} // namespace asn1

在上述结构中声明的成员函数“pack”在另一个文件中定义。 "rrc_nbiot.cc"

// SystemInformationBlockType2-NB-r13 ::= SEQUENCE
SRSASN_CODE sib_type2_nb_r13_s::pack(bit_ref& bref) const
{
  /*  some code */
  return SRSASN_SUCCESS;
}

在我的主文件“main.cpp”中,我实例化结构并尝试调用成员函数:

#include "srsran/asn1/rrc_nbiot.h"
struct asn1::rrc::sib_type2_nb_r13_s sib2;
struct asn1::rrc::sib_type2_nb_r13_s *sib2_ref = &sib2;

int main(int argc, char** argv)
{

  uint8_t buf[1024];
  uint32_t nof_bytes = 2;
  asn1::bit_ref  bref;

  sib2_ref->pack(&bref);
...

当我编译时,我得到了错误:

" 错误:没有匹配的调用函数 'asn1::rrc::sib_type2_nb_r13_s::pack(asn1::bit_ref*)'
sib2_ref->pack(&bref); "

尽管您在头文件中看到“pack”明确声明在“sib_type2_nb_r13_s”下。 结构的其他成员不是我可以毫无问题地访问和修改的函数,但是调用像“pack”这样的成员函数会给我这个错误。

感谢任何帮助

【问题讨论】:

  • 您正在尝试将bit_ref* 传递给采用bit_ref&amp; 的函数,这(很明显)不匹配。也许您需要阅读您最喜欢的 C++ 书籍中的指针和引用之间的区别。
  • 你传递的是一个指针,而asn1::rrc::sib_type2_nb_r13_s::pack 接受一个引用。
  • 我的一些问题:为什么要将 sib2 创建为全局对象?当您可以直接使用 sib2 时,为什么还要创建指向 sib2 的指针?你在不需要的地方增加了复杂性。
  • @molbdnilo,不,我将引用 &bref 传递给函数
  • @PepijnKramer,我没有理由只是做出不同的尝试来让事情顺利进行。尽管如此,我还是尝试使用实例“sib2”和指向实例“sib2_ref”的指针来访问成员函数,但两次尝试都给出了相同的错误。

标签: c++ oop struct constructor


【解决方案1】:

感谢 cmets 中的 @molbdnilo,我修复了错误。就像他说的那样。

当我调用成员函数时,我不应该将参数作为指针传递给我。 参数应该是这样的:

  sib2_ref->pack(bref);

【讨论】:

    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多