1

为 Arduino 编写新库后,编译器无法识别这个新类并返回错误,

BinaryCounter:15:错误:“二进制”未命名类型

以下是产生错误的文件。类在 中定义Binary.h、实现Binary.cpp和使用BinaryCounter.ino

二进制.h

#ifndef Binary_h
#define Binary_h

#include "Arduino.h"

class Binary {
  public:
    Binary (int pins[], int numPins);
    void display(int number);
    void clear();
  private:
    boolean *_values;
    int *_pins;
    int _numPins;
    void _update();
    void _setValues (int num);
};

#endif

二进制文件

#include "Binary.h"

Binary::Binary(int pins[], int numPins) {
  _values = new boolean[numPins];
  _pins = pins;
  _numPins = numPins;

  for (int i = 0; i < _numPins; i ++) {
    pinMode(_pins[i], OUTPUT);
    _values[i] = false;
  }

  _update();
}

void Binary::_update() {
  for (int i = 0; i < _numPins; i ++) {
    if (_values[i]) {
      digitalWrite(_pins[i], HIGH);
    } else {
      digitalWrite(_pins[i], LOW);
    }
  }
}

void Binary::_setValues (int num) {
  for (int i = 0; i < _numPins; i ++, num/=2) {
    _values[i] = ((num%2)==1);
  }
}

void Binary::display (int number) {
  _setValues(number);
  _update();
}

void Binary::clear() {
  display(0);
}

和 BinaryCounter.ino

#include <Binary.h>

int pins[] = {2,3,4,5,6};
int numPins = 5;

Binary dis(pins, numPins);

void setup() {
}

void loop() {  
  int loopEnd = 1<<numPins;
  for (int i = 0; i < loopEnd; i ++) {
    dis.display(i);
    delay(200);
  }
}
4

2 回答 2

5

更改<Binary.h>为文件 BinaryCounter 中的引号"Binary.h"

于 2012-06-12T06:57:59.727 回答
0

.cpp 和 .h 是否在 library/BinaryCounter/ 目录中?您是否创建了keywords.txt 文件?

于 2012-08-19T20:09:30.800 回答