简体   繁体   中英

Getting the error 'does not name type' while programming a custom class for an Arduino

After programming a new library for a Arduino, the compiler does not recognize this new class and returns the error,

BinaryCounter:15: error: 'Binary' does not name a type

Here are the files that produce the error. The class is defined in Binary.h , implemented in Binary.cpp and used in BinaryCounter.ino

Binary.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

Binary.cpp

#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);
}

and 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);
  }
}

更改<Binary.h>以在文件BinaryCounter中引用"Binary.h"

Are the .cpp and .h in libraries/BinaryCounter/ directory? Did you created the keywords.txt file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM