无法为 Encoder.h 构建包装器 class,在调用包装器 class 构造器之前调用了编码器构造器
Cant build a wrapper class for Encoder.h, Encoder constructor has been called before wrapper class constructor is called
这是我的MotorEncoder.h
#include <Encoder.h>
class MotorEncoder {
public:
MotorEncoder(int pin1, int pin2);
void init();
int read();
private:
int pin1;
int pin2;
Encoder encoder;
};
这是MotorEncoder.cpp
#include "MotorEncoder.h"
MotorEncoder::MotorEncoder(int pin1, int pin2) {
this->pin1 = pin1;
this->pin2 = pin2;
this->encoder = Encoder(pin1, pin2);
}
void MotorEncoder::init() {
}
int MotorEncoder::read() {
}
我总是收到一条错误消息,指出我使用 0 个参数调用编码器。
In file included from src/encoder/MotorEncoder.h:5:0,
from src/encoder/MotorEncoder.cpp:1:
/Users/slawalata/.platformio/lib/Encoder_ID129/Encoder.h:72:2: note: candidate: Encoder::Encoder(uint8_t, uint8_t)
Encoder(uint8_t pin1, uint8_t pin2) {
^
/Users/slawalata/.platformio/lib/Encoder_ID129/Encoder.h:72:2: note: candidate expects 2 arguments, 0 provided
它在编译时中断。我根本没有调用过这个构造函数。
非常感谢。
基于此 link,我将代码更改为:
MotorEncoder.h
class MotorEncoder {
public:
MotorEncoder(int pin1, int pin2);
void init();
int read();
private:
int pin1;
int pin2;
Encoder* encoder;
};
MotorEncoder.cpp
MotorEncoder::MotorEncoder(int pin1, int pin2) {
this->pin1 = pin1;
this->pin2 = pin2;
this->encoder = new ::Encoder(pin1, pin2);
}
void MotorEncoder::init() {
}
int MotorEncoder::read() {
return encoder->read();
}
非常感谢,
这是我的MotorEncoder.h
#include <Encoder.h>
class MotorEncoder {
public:
MotorEncoder(int pin1, int pin2);
void init();
int read();
private:
int pin1;
int pin2;
Encoder encoder;
};
这是MotorEncoder.cpp
#include "MotorEncoder.h"
MotorEncoder::MotorEncoder(int pin1, int pin2) {
this->pin1 = pin1;
this->pin2 = pin2;
this->encoder = Encoder(pin1, pin2);
}
void MotorEncoder::init() {
}
int MotorEncoder::read() {
}
我总是收到一条错误消息,指出我使用 0 个参数调用编码器。
In file included from src/encoder/MotorEncoder.h:5:0,
from src/encoder/MotorEncoder.cpp:1:
/Users/slawalata/.platformio/lib/Encoder_ID129/Encoder.h:72:2: note: candidate: Encoder::Encoder(uint8_t, uint8_t)
Encoder(uint8_t pin1, uint8_t pin2) {
^
/Users/slawalata/.platformio/lib/Encoder_ID129/Encoder.h:72:2: note: candidate expects 2 arguments, 0 provided
它在编译时中断。我根本没有调用过这个构造函数。
非常感谢。
基于此 link,我将代码更改为:
MotorEncoder.h
class MotorEncoder {
public:
MotorEncoder(int pin1, int pin2);
void init();
int read();
private:
int pin1;
int pin2;
Encoder* encoder;
};
MotorEncoder.cpp
MotorEncoder::MotorEncoder(int pin1, int pin2) {
this->pin1 = pin1;
this->pin2 = pin2;
this->encoder = new ::Encoder(pin1, pin2);
}
void MotorEncoder::init() {
}
int MotorEncoder::read() {
return encoder->read();
}
非常感谢,