对象引用中的字段以不同的方法返回不同的值

Field in object reference returns different values in different methods

本文关键字:方法 返回 字段 对象引用      更新时间:2023-10-16

我使用Arduino IDE并为色彩管理编写了一些类。如果我使用 ColorManager 对象初始化推子,我会根据访问位置获得不同的颜色值。如果我尝试访问构造函数中的对象引用,我会得到正确的值。如果我在其他类方法中做同样的事情,我会得到错误的值。所需的行为应该是始终返回相同值的代码,与访问位置无关。

我很困惑,因为如果我尝试访问产生错误输出的方法之后的值,输出就会发生变化。

curColor.getRed();代码行更改Fader::doStep(...)方法的输出。这绝对没有意义,因为ColorManager::getRed()方法根本不接触对象属性/变量。

我试图找出有问题的部分几个小时,但找不到它。下面我发布了有问题的代码。因为我找不到产生错误的部分,所以我无法进一步减少示例。

我已经用注释标记了代码中有问题的部分(//(

主要:

#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;
}
void loop() {
ColorManager curColor = to_rgb(255,0,0);
Fader f = Fader(to_rgb(255,0,0),to_rgb(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

色彩经理.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);
}

推子.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); // right value 1.00
Serial.println(startColor.red); // right value 1.00
}
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);   // wrong value 0.00
Serial.println(this->startColor.getRed()); // right value 255
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;
}

Fader f = Fader(to_rgb(255,0,0),to_rgb(0,255,0));将临时对象作为参数传递。存储对这些对象的引用(如const ColorManager& startColor;const ColorManager& endColor;(会使对象在传递的对象超出范围后(它们在构造函数调用结束时这样做(并访问这些调用未定义的行为

最简单的解决方法是将成员更改为值的副本,例如:const ColorManager startColor;const ColorManager endColor;