c++ 在类中传递对另一个类的引用时出现问题

wi3ka0sx  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(119)

我创建了下面的类,它有一个通过引用传入的对象,并以下面的方式实现。

Diablo_Serial_4DLib Display(&DisplaySerial);

ZenDisplay ui(Display);
class ZenDisplay
{
  public:
    ZenDisplay(Diablo_Serial_4DLib &display);
    void setup();
  private:
    Diablo_Serial_4DLib* _display;
};

构造函数是直来直去的,按预期工作,到目前为止没有问题。

// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display)
{
    _display = &display;
}

我想示例化ZenSpeakerGroup类并将相同的引用传递到它的构造函数中
一个三个三个一个
我把原来的工人阶级修改成了

class ZenDisplay
{
  public:
    ZenDisplay(Diablo_Serial_4DLib &display);
    void setup();
  private:
    Diablo_Serial_4DLib* _display;
    ZenSpeakerGroup _speakerGroup;
};
// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
{
    _display = &display;
}

现在我得到了下面的错误,并不是100%确定我做错了什么。

Arduino: 1.8.16 (Mac OS X), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None"

/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp: In constructor 'ZenDisplay::ZenDisplay(Diablo_Serial_4DLib&)':
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:14:78: error: no matching function for call to 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib*)'
 ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
                                                                              ^
In file included from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.h:14,
                 from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:9:
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note: candidate: 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib&)'
     ZenSpeakerGroup(Diablo_Serial_4DLib &display);
     ^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note:   no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'Diablo_Serial_4DLib&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(const ZenSpeakerGroup&)'
 class ZenSpeakerGroup
       ^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note:   no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'const ZenSpeakerGroup&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(ZenSpeakerGroup&&)'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note:   no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'ZenSpeakerGroup&&'
exit status 1
Error compiling for board DOIT ESP32 DEVKIT V1.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

我也尝试了以下方法,但没有成功。_speakerGroup = new ZenSpeakerGroup(&display)我将非常感谢在这方面的一些帮助。

0g0grzrc

0g0grzrc1#

这是将显示器引用传递给ZenSpeakerGroup的正确方法

ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(display)
{
    _display = &display;
}
wljmcqd8

wljmcqd82#

我也犯了这个错误,这是个打字错误
确保当你要初始化一个引用时将它定义为一个引用

class ZenDisplay{
 public:
  ZenDisplay(Diablo_Serial_4DLib &display);
  void setup();
 private:
  Diablo_Serial_4DLib& _display; // <--make sure this is a refference
 };

现在初始化引用

ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display){
  _display = display;
}

相关问题