gpt4 book ai didi

c++ - 为 Arduino 编写自定义类时出现错误 'does not name type'

转载 作者:行者123 更新时间:2023-11-30 01:26:09 33 4
gpt4 key购买 nike

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

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

以下是产生错误的文件。该类在 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

二进制.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);
}

和 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>引用"Binary.h"在文件 BinaryCounter 中。

关于c++ - 为 Arduino 编写自定义类时出现错误 'does not name type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10991317/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com