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

C++ C++;对象引用返回不同的值,c++,class,oop,arduino,C++,Class,Oop,Arduino,我使用ArduinoIDE并编写了一些用于颜色管理的类:Fader和ColorManager。如果使用ColorManager对象初始化音量控制器,我会根据访问位置获得不同的颜色值。如果我尝试访问构造函数中的对象引用,我会得到正确的值。如果我在其他类方法中也这样做,我会得到错误的值 我是做错了什么,还是这是一个g++/C++错误 主要内容: #包括 #定义蓝色引脚3 #定义绿色引脚5 #定义红色_引脚6 无效设置(){ Serial.begin(9600); 而(!Serial){} 引脚模式(

我使用ArduinoIDE并编写了一些用于颜色管理的类:Fader和ColorManager。如果使用ColorManager对象初始化音量控制器,我会根据访问位置获得不同的颜色值。如果我尝试访问构造函数中的对象引用,我会得到正确的值。如果我在其他类方法中也这样做,我会得到错误的值

我是做错了什么,还是这是一个g++/C++错误

主要内容:
#包括
#定义蓝色引脚3
#定义绿色引脚5
#定义红色_引脚6
无效设置(){
Serial.begin(9600);
而(!Serial){}
引脚模式(红色引脚,输出);
引脚模式(绿色引脚,输出);
引脚模式(蓝色引脚,输出);
pinMode(7,输入\上拉);
}
#定义GAMMA 1.0f
#定义色调为红色1.0f
#定义色调为绿色1.0f
#定义色调为蓝色1.0f
颜色管理器至rgb(uint8\u t r、uint8\u t g、uint8\u t b){
ColorManager颜色=ColorManager(r、g、b);
返回颜色;
}
ColorManager stColor=ColorManager((uint8_t)255,0,0);
void循环(){
ColorManager&curColor=stColor;
音量控制器f=音量控制器(ColorManager((uint8_t)255,0,0),ColorManager((uint8_t)0255,0));
curColor=f.doStep();
curColor.getRed();//此调用更改输出
而(1){}
}
班级 标题
\ifndef ColorManager\u H
#定义颜色管理器
#包括
#包括
类颜色管理器{
公众:
色彩管理器(uint8_t r、uint8_t g、uint8_t b);
ColorManager(浮子r、浮子g、浮子b);
ColorManager(浮动红=1.0,浮动绿=0.0,浮动蓝=0.0,浮动色域校正=1.0,浮动色域=1.0,浮动色域=1.0,浮动色域=1.0,浮动色域=1.0,uint16μt m=255);
uint16_t getGreen()常量;
uint16_t getBlue()常量;
uint16_t getRed()常量;
uint16_t getBrightness()常量;
ColorManager操作员*(浮点b);
无效设置伽马校正(浮动伽马);
无效设置校正(浮动r、浮动g、浮动b);
void setMax(uint16_t m);
uint16_t getMax()常量;
飘红;
浮绿;
飘蓝;
受保护的:
常量颜色管理器*norm();
私人:
浮华的色彩;
浮漂火人;
浮华湖蓝;
浮动伽玛校正;
uint16_t m;
};
类音量控制器{
公众:
音量控制器(康斯特色彩管理器和a、康斯特色彩管理器和b);
const ColorManager&doStep();
void setInterpolation(uint8_t(*步进器)(uint8_t));
无效设置相移(int移位);
bool fadeFinished();
uint8_t getPhase()常量;
int getShift()常量;
无效重置();
浮动斜坡;
浮动斜坡b;
浮动斜坡;
受保护的:
静态uint8\u t defaultIntp(uint8\u t步骤);
私人:
uint8_t相;
int移位;
布尔开始;
uint8_t(*步进器)(uint8_t);
const ColorManager&startColor;
const ColorManager&endColor;
颜色管理器当前颜色;
};
#恩迪夫
ColorManager.cpp
#包括
#包括
#包括
#包括
#包括
#定义最大输出255
常量ColorManager*ColorManager::norm(){
此->红色/=(浮动)255;
此->绿色/=(浮动)255;
此->蓝色/=(浮动)255;
归还这个;
}
ColorManager::ColorManager(uint8\u t r、uint8\u t g、uint8\u t b):红色(r)、绿色(g)、蓝色(b)、m(255){
这->norm();
setGammaCorrection(1);
setHueCorrection(1,1,1);
}
ColorManager::ColorManager(浮点r、浮点g、浮点b):红色(r)、绿色(g)、蓝色(b)、m(255){
setGammaCorrection(1);
setHueCorrection(1,1,1);
}
颜色管理器:颜色管理器(浮动红、浮动绿、浮动蓝、浮动色差校正、浮动色差、浮动色差、浮动色差、uint16_t m):红(红)、绿(绿)、蓝(蓝)、色差校正(色差校正)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差校正(色差)、色差(色差)、色差(色差)
void ColorManager::setGammaCorrection(浮点gamma){
这->伽马校正=伽马;
}
void ColorManager::setHueCorrection(浮点r、浮点g、浮点b){
这个->Huerd=r;
这->霍格伦=g;
这->hueBlue=b;
}
void ColorManager::setMax(uint16\u t m){
这个->m=m;
}
uint16\u t颜色管理器::getMax()常量{
返回此->m;
}
uint16\u t ColorManager::getRed()常量{
浮动r=此->红色;
r=pow(r,this->gammaCorrection)*this->m*this->hueRed;
返回轮(r);
}
uint16\u t颜色管理器::getGreen()常量{
绿色浮动=此->绿色;
绿色=战力(绿色,本->伽马矫正)*本->米*本->火影;
返回轮(绿色);
}
uint16\u t颜色管理器::getBlue()常量{
蓝色浮动=此->蓝色;
蓝色=战力(蓝色,这个->伽马矫正)*这个->米*这个->火蓝;
返回轮(蓝色);
}
uint16\u t ColorManager::getBrightness()常量{
返回sqrt(pow(红色*本->火色,2)+pow(绿色*本->火色,2)+pow(蓝色*本->火色,2))*本->米;
}
ColorManager ColorManager::运算符*(浮点b){
返回颜色管理器(这个->红色*b,这个->绿色*b,这个->蓝色*b,这个->gammaCorrection,这个->hueRed,这个->hueGreen,这个->hueBlue);
}
音量控制器
#包括
#包括
#包括
音量控制器::音量控制器(常量颜色管理器&a、常量颜色管理器&b):开始颜色(a)、结束颜色(b)、当前颜色(a)、相位(0)、步进器(&Fader::defaultIntp)、移位(0)、开始(false){
这个->渐变r=(b.red-a.red)/100;
这个->斜坡g=(b.green-a.green)/100;
这个->渐变b=(b.blue-a.blue)/100;
序列号:println(a.red);
}
uint8\u t音量控制器::defaultIntp(uint8\u t步长){
返回步骤;
}
无效音量控制器::设置插值(uint8_t(*步进器)(uint8_t)){
这个->步进器=步进器;
}
无效音量控制器::设置相位偏移(整数偏移){
这个->移位=移位;
}
bool音量控制器::fadeFinished(){
返回此->步进器(此->相位)==100;
}
常量颜色管理器和音量控制器::doStep(){
uint8\u t映射相位=此->步进器(此->相位+此->移位);
如果(相位<100){
该->阶段=++阶段;
}
Serial.println(此->startColor.red);
this->currentColor.red=this->startColor.red+this->ramp\u r*mappedPhase;
this->currentColor.green=this->startColor.green+this->ramp\u g*mappedPhase;
this->currentColor.blue=this->startColor.blue+this->ramp_b*mappedPhase;
返回此->当前颜色;
}
uint8\u t音量控制器::getPhase()常量{
ret
#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;
}
Fader f = Fader(ColorManager((uint8_t) 255,0,0),ColorManager((uint8_t) 0,255,0));
const ColorManager& startColor;
const ColorManager& endColor;