gpt4 book ai didi

c++ - 错误:没有用于调用 Counter::Counter() 的匹配函数

转载 作者:太空狗 更新时间:2023-10-29 20:35:20 28 4
gpt4 key购买 nike

我在尝试编译 Arduino 草图时遇到此错误。我看不出它认为我试图在没有参数的情况下调用 Counter::Counter 的地方。这是怎么回事?

sketch/periodic_effect.cpp: In constructor 'PeriodicEffect::PeriodicEffect(uint32_t, uint32_t, Counter::Mode)':
periodic_effect.cpp:4: error: no matching function for call to 'Counter::Counter()'
PeriodicEffect::PeriodicEffect( uint32_t durationMs, uint32_t periods, Counter::Mode mode) : Runnable(), periods(periods) {

periodic_effect.cpp

#include <Arduino.h>
#include "periodic_effect.h"

PeriodicEffect::PeriodicEffect( uint32_t durationMs, uint32_t periods, Counter::Mode mode) : Runnable(), periods(periods) {
intervalMs = durationMs / periods;
index = Counter(intervalMs, mode);
}


void PeriodicEffect::run() {
uint32_t currTimeMs = millis();
if( (currTimeMs - prevTimeMs) > intervalMs ) {
applyChange();
prevTimeMs = currTimeMs;
index.increment();
}
}

periodic_effect.h

#ifndef PERIODIC_EFFECT_H
#define PERIODIC_EFFECT_H

#include "counter.h"
#include "runnable.h"

class PeriodicEffect : public Runnable {

public:
PeriodicEffect( uint32_t durationMs, uint32_t periods, Counter::Mode mode = Counter::Mode::RESETTING );

Counter index;

private:
uint32_t intervalMs, periods, prevTimeMs;

virtual void applyChange() = 0;
void run();
virtual void setup() = 0;
};

#endif

counter.h

#ifndef COUNTER_H
#define COUNTER_H

#include <stdint.h>


class Counter {
public:
enum Mode { RESETTING, RETURNING };
Counter(uint32_t limit, Counter::Mode mode = RESETTING, uint32_t index = 0);

uint32_t getIndex();
void increment();
float percent();

private:
bool goingForward;
uint32_t index;
const uint32_t limit;
Counter::Mode mode;

void incrementReturning();
void incrementResetting();

};

#endif

计数器.cpp

#include "counter.h"

Counter::Counter(uint32_t limit, Counter::Mode mode, uint32_t index) : index(index), limit(limit), mode(mode) {
goingForward = true;
}


uint32_t Counter::getIndex() {
return index;
}


void Counter::increment() {
switch( mode ) {
case RESETTING : incrementResetting();
case RETURNING : incrementReturning();
}
}


void Counter::incrementResetting() {
index = (index + 1) % limit;
}


void Counter::incrementReturning() {
if( goingForward ) {
index++;
if( index >= limit ) {
index = limit - 1;
goingForward = false;
}
} else {
index--;
if( index <= 0 ) {
index = 1;
goingForward = true;
}
}
}


float Counter::percent() {
return (float) index / (float) limit;
}

最佳答案

您的类 PeriodicEffect 有一个 Counter 成员变量,但没有构造函数。这意味着成员变量将需要默认构造,即由不带参数的构造函数构造。你还没有提供。

您可以为Counter 提供默认构造函数,或者让现有构造函数为所有 参数设置默认值。或者,您可以创建一个带有初始化列表的 PeriodicEffect 构造函数,以向 index 提供适当的参数。

关于c++ - 错误:没有用于调用 Counter::Counter() 的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43195306/

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