【问题标题】:C++ template class as argument of a functionC++ 模板类作为函数的参数
【发布时间】:2017-05-01 01:46:57
【问题描述】:

在 MIDI.h 中定义了一个类库(我不能/不应该更改):

template<class SerialPort, class _Settings = DefaultSettings>
class MidiInterface
{
      ...

我想用这个类作为参数。 问题question 重复,但我仍然收到编译错误。

我自己的头类文件长这样:

#include <MIDI.h>

class M
{
  public:
    void setup();
    void run();

  private:
    void printMessage(const MidiInterface<SerialPort, _Settings>&
          midiInterface);
};

我收到以下错误:

在草图\M.cpp:1:0: 中包含的文件中:

M.h:10: error: 'MidiInterface' does not name a type

 void printMessage(const MidiInterface<SerialPort, _Settings>& midiInterface);

                         ^

M.h:10: error: expected ',' or '...' before '<' token

 void printMessage(const MidiInterface<SerialPort, _Settings>& midiInterface);

                                      ^

exit status 1
'MidiInterface' does not name a type

如何更改代码以便编译?

【问题讨论】:

    标签: c++ templates arduino


    【解决方案1】:

    看起来this code 中的 MIDI.h 将所有内容都放在了一个命名空间中。如果这就是您正在使用的,请尝试MIDI_NAMESPACE::MidiInterface&lt;SerialPort, _Settings&gt;

    【讨论】:

    • 支持 +1,因为找到原始来源真的很顺利
    • @aschepler:在你回答之后我更新了我的问题,我什至没有提到 Arduino,你还是找到了它。它现在可以工作(在您的帮助下),我还必须进行更多更改(例如设置和串行端口,但由于您的回答和指路,它可以编译)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多