C ++多态打印功能无法正确解析

时间:2016-11-19 01:26:44

标签: c++ json arduino polymorphism teensy

我创建了一个类,它通过创建一个抽象类Print.h来抽象一个名为SerialFlash的SPI闪存芯片库。当我尝试使用ArduinoJson库打印到这个时,我收到一个错误:

src/FlashMemory.cpp:99:36: error: no matching function for call to 'ArduinoJson::JsonObject::printTo(<unresolved overloaded function type>)'
root.printTo(serialFlashPrint);
^

lib/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:34:10: note: size_t ArduinoJson::Internals::JsonPrintable<T>::printTo(Print&) const [with T = Ardu
inoJson::JsonObject; size_t = unsigned int]
size_t printTo(Print &print) const {
^
lib/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:34:10: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 
'Print&'

上面错误中引用的文件位于:https://github.com/bblanchon/ArduinoJson/blob/master/include/ArduinoJson/Internals/JsonPrintable.hpp

这是该类的头文件:

#include <Arduino.h>
#include <SerialFlash.h>
#include "Print.h"

#ifndef _SerialFlashPrint_h_
#define _SerialFlashPrint_h_

class SerialFlashPrint : public Print {
  public:
    SerialFlashPrint(SerialFlashFile *file);

    virtual size_t write(uint8_t);
    virtual size_t write(const uint8_t *buffer, size_t size);

  private:
    char buf[1];
    uint16_t _current_byte;
    SerialFlashFile * _file;
};

#endif

和cpp文件:

#include "serialFlashPrint.h"

SerialFlashPrint::SerialFlashPrint(SerialFlashFile * file) : Print() {
  this->_file = file;
  this->_current_byte = 0;
}

size_t SerialFlashPrint::write(uint8_t c) {
  if(_current_byte == 0){
    _file->erase();
    _file->seek(0);
  }
  sprintf(buf, "%c", c);
  _file->write(buf, 1);
  _current_byte++;
  return 0;
}

size_t SerialFlashPrint::write(const uint8_t *buffer, size_t size){
  _file->erase();
  _file->seek(0);
  _file->write(buffer, size);
  _file->write(NULL, 1);
  return 0;
};

通常,您使用print函数:root.printTo(Serial)。这段代码基于一个抽象(我之前已经开始工作),称为Chunked输出,可以在这里看到:https://github.com/bblanchon/ArduinoJson/wiki/Bag-of-Tricks

有没有人有任何线索让我弄清楚为什么我会收到<unresolved overloaded function type>而非Print&amp;?

1 个答案:

答案 0 :(得分:0)

<unresolved overloaded function type>表示编译器发现了一个具有多个重载的函数,并且不知道要使用哪个函数。

您的代码或库中很可能有多个serialFlashPrint()

如果没有,那么您可能已触发Most vexing parse

SerialFlashPrint serialFlashPrint;   // <- creates an instance of SerialFlashPrint  
SerialFlashPrint serialFlashPrint(); // <- declares a function returning a SerialFlashPrint