C ++对象引用返回不同的值

时间:2019-12-21 11:49:08

标签: c++ class oop arduino

我使用Arduino IDE并为颜色管理编写了一些类:Fader和ColorManager。如果使用ColorManager对象初始化一个推子,我会根据访问位置获得不同的颜色值。如果尝试访问构造函数中的对象引用,则将获得正确的值。如果我在其他类方法中执行相同的操作,则会得到错误的值。

我是在做错什么还是这是g ++ / C ++错误?

主要:

#include <ColorManager.h>

#define BLUE_PIN 3
#define GREEN_PIN 5
#define RED_PIN 6

void setup() {
  Serial.begin(9600);
  while(!Serial) {}
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(7,INPUT_PULLUP);
}

#define GAMMA 1.0f
#define HUE_RED 1.0f
#define HUE_GREEN 1.0f
#define HUE_BLUE 1.0f

ColorManager to_rgb(uint8_t r,uint8_t g, uint8_t b) {
  ColorManager color = ColorManager(r,g,b);
  return color;
}

ColorManager stColor = ColorManager((uint8_t)255,0,0);

void loop() {
  ColorManager& curColor = stColor;
  Fader f = Fader(ColorManager((uint8_t) 255,0,0),ColorManager((uint8_t) 0,255,0));
  curColor = f.doStep();
  curColor.getRed(); // This call changes the output
  while(1) {}
}

标题

#ifndef ColorManager_H
#define ColorManager_H
#include <math.h>
#include <stdint.h>
class ColorManager {
public:
    ColorManager(uint8_t r,uint8_t g, uint8_t b);
    ColorManager(float r, float g, float b);
    ColorManager(float red = 1.0, float green = 0.0, float blue = 0.0, float gammaCorrection = 1.0, float hueRed = 1.0, float hueGreen = 1.0, float hueBlue = 1.0, uint16_t m = 255);
    uint16_t getGreen() const;
    uint16_t getBlue() const;
    uint16_t getRed() const;
    uint16_t getBrightness() const;
    ColorManager operator* (float b);
    void setGammaCorrection(float gamma);
    void setHueCorrection(float r, float g, float b);
    void setMax(uint16_t m);
    uint16_t getMax() const;
    float red;
    float green;
    float blue;

protected:
    const ColorManager* norm();
private:

    float hueRed;
    float hueGreen;
    float hueBlue;
    float gammaCorrection;
    uint16_t m;
};

class Fader {
public:
    Fader(const ColorManager& a, const ColorManager& b);
    const ColorManager& doStep();
    void setInterpolation(uint8_t (*stepper)(uint8_t));
    void setPhaseShift(int shift);
    bool fadeFinished();
    uint8_t getPhase() const;
    int getShift() const;
    void reset();
    float ramp_r;
    float ramp_b;
    float ramp_g;
protected:
    static uint8_t defaultIntp(uint8_t step);
private:
    uint8_t phase;
    int shift;
    bool started;
    uint8_t (*stepper)(uint8_t);
    const ColorManager& startColor;
    const ColorManager& endColor;
    ColorManager currentColor;
};
#endif

ColorManager.cpp

#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <Arduino.h>
#include <ColorManager.h>
#define MAX_OUTPUT 255
const ColorManager* ColorManager::norm() {
    this->red /= (float) 255;
    this->green /= (float) 255;
    this->blue /= (float) 255;
    return this;
}

ColorManager::ColorManager(uint8_t r, uint8_t g, uint8_t b) : red(r),green(g),blue(b),m(255) {
    this->norm();
    setGammaCorrection(1);
    setHueCorrection(1,1,1);
}

ColorManager::ColorManager(float r, float g, float b) : red(r),green(g),blue(b),m(255) {
    setGammaCorrection(1);
    setHueCorrection(1,1,1);
}
ColorManager::ColorManager(float red, float green, float blue, float gammaCorrection, float hueRed, float hueGreen, float hueBlue,uint16_t m) : red(red),green(green),blue(blue),gammaCorrection(gammaCorrection),hueRed(hueRed),hueGreen(hueGreen),hueBlue(hueBlue),m(m) {}

void ColorManager::setGammaCorrection(float gamma) {
    this->gammaCorrection = gamma;
}

void ColorManager::setHueCorrection(float r, float g, float b) {
    this->hueRed = r;
    this->hueGreen = g;
    this->hueBlue = b;
}

void ColorManager::setMax(uint16_t m) {
    this->m = m;
}

uint16_t ColorManager::getMax() const {
    return this->m;
}

uint16_t ColorManager::getRed() const {
    float r = this->red;
    r = pow(r,this->gammaCorrection) * this->m * this->hueRed;
    return round(r);
}

uint16_t ColorManager::getGreen() const {
    float green = this->green;
    green = pow(green, this->gammaCorrection) * this->m * this->hueGreen;
    return round(green);
}

uint16_t ColorManager::getBlue() const {
    float blue = this->blue;
    blue = pow(blue,this->gammaCorrection) * this->m * this->hueBlue;
    return round(blue);
}
uint16_t ColorManager::getBrightness() const {
    return sqrt(pow(red * this->hueRed,2)+pow(green*this->hueGreen,2)+pow(blue*this->hueBlue,2)) * this->m;
}

ColorManager ColorManager::operator*(float b) {
    return ColorManager(this->red * b, this->green * b, this->blue * b, this->gammaCorrection, this->hueRed, this->hueGreen, this->hueBlue);
}

Fader.cpp

#include <ColorManager.h>
#include <Arduino.h>
#include <math.h>
Fader::Fader(const ColorManager& a, const ColorManager& b) : startColor(a),endColor(b),currentColor(a),phase(0),stepper(&Fader::defaultIntp),shift(0),started(false) {
    this->ramp_r = (b.red - a.red)/100;
    this->ramp_g = (b.green - a.green)/100;
    this->ramp_b = (b.blue - a.blue)/100;
    Serial.println(a.red);
}

uint8_t Fader::defaultIntp(uint8_t step) {
    return step;
}

void Fader::setInterpolation(uint8_t (*stepper)(uint8_t)) {
    this->stepper = stepper;
}

void Fader::setPhaseShift(int shift) {
    this->shift = shift;
}

bool Fader::fadeFinished() {
    return this->stepper(this->phase) == 100;
}

const ColorManager& Fader::doStep() {
    uint8_t mappedPhase = this->stepper(this->phase + this->shift);
    if (mappedPhase < 100) {
        this->phase = ++phase;
    }
    Serial.println(this->startColor.red);
    this->currentColor.red = this->startColor.red + this->ramp_r * mappedPhase;
    this->currentColor.green = this->startColor.green + this->ramp_g * mappedPhase;
    this->currentColor.blue = this->startColor.blue + this->ramp_b * mappedPhase;
    return this->currentColor;
}

uint8_t Fader::getPhase() const {   
    return this->phase;
}

int Fader::getShift() const {
    return this->shift;
}

void Fader::reset() {
    this->phase = 0;
    this->currentColor = this->startColor;
}

1 个答案:

答案 0 :(得分:1)

此行导致悬空引用:

Fader f = Fader(ColorManager((uint8_t) 255,0,0),ColorManager((uint8_t) 0,255,0));

参数是临时的,但是Fader存储对参数的引用

const ColorManager& startColor;
const ColorManager& endColor;

因此,在执行了该行之后,这些引用将引用不再存在的临时对象,从而导致当您尝试在doStep中使用它们时导致未定义的行为。

这是您在考虑提供班级参考成员时应始终注意的一个陷阱。另一个类似的陷阱是,该类将无法分配或移动,因为您无法“重新安置”这些引用

要解决此问题,您可以将成员设为ColorManager(不是const而不是引用)。或者,您可以将f的初始化更改为不使用临时文件。