c++ - 你能在函数中声明一个常量对象吗?

标签 c++ arduino-ide uninitialized-constant fastled

我完成了一个学期的 Comp Sci,这里什么都没有。

我需要调用一个常量 int 以用于 FastLED strip 中的 LED 数量。长话短说,我正在尝试替换...

    #include "FastLED.h"
    #define NUM_LEDS_RT 174
    #define DATA_PIN_RT 6 
    #define CLOCK_PIN_RT 5
    ...
    CRGB led[NUM_LEDS_RT + NUM_LEDS_MID + NUM_LEDS_LF + NUM_LEDS_FIRE];
    FastLED.addLeds<WS2801, DATA_PIN_RT, CLOCK_PIN_RT, BGR> (led, NUM_LEDS_RT);

使用我自己的库,我为 LED 灯条创建了一个结构,保存上述信息,由程序的驱动程序文件播种。一切都应该正常工作,但我无法克服第一个错误。例如,包含我的数据的对象是由...构建的

    Strip::Strip(int data, int clock, int num)
    {
       #define DATA_Pin data
       #define CLOCK_Pin clock
       #define NUM_LEDS num
    }

并由驱动程序播种为...

    Strip RIGHT(6, 5, 174);

简单来说,有没有办法让一个对象拥有常量值呢?发生的错误表明 RIGHT.name 不是常量变量,并收到“Led.cpp:39: error: wrong number of template arguments (4, should be 3)”或它的一些变体。

求助!如果您需要任何进一步的信息,请告诉我。谢谢!

编辑:添加了更多代码以供引用: 脱衣类

    #ifndef Strip_h
    #define Strip_h
    #include "Arduino.h"
    #include <String>
    #include "C:/Program Files (x86)/Arduino/libraries/FastLED/FastLED.h"

    class Strip
    {
    public: 
Strip(int data, int clock, int num);

    private:
    int DATA_Pin;
int CLOCK_Pin;
int NUM_LEDS;
    };

    #endif

    #include "Strip.h"


    Strip::Strip(int data, int clock, int num)
    {
         DATA_Pin = data;
         CLOCK_Pin = clock;
         NUM_LEDS = num;
    }

带领类

    #ifndef Led_h
    #define Led_h
    include "C:/Program Files (x86)/Arduino/libraries/FastLED/FastLED.h"
    #include "Strip.h"

    class Led
    {
    public: 
        Led(const Strip& name);
        Led(const Strip& name, const Strip& name2);
        Led(const Strip& name, const Strip& name2, const Strip& name3);
        Led(const Strip& name, const Strip& name2, const Strip& name3, const Strip& name4);                        

    private:

    };

    #endif

    Led::Led(const Strip& const Strip&, const Strip& name2, const Strip& name3, const Strip& name4)
    {
        CRGB led[name.NUM_LEDS + name2.NUM_LEDS + name3.NUM_LEDS + name4.NUM_LEDS];

        FastLED.addLeds<WS2801, name.DATA_Pin, name.CLOCK_Pin, BGR>(led, name.NUM_LEDS);
        FastLED.addLeds<WS2801, name2.DATA_Pin, name2.CLOCK_Pin, BGR>(led, name.NUM_LEDS, name.NUM_LEDS + name2.NUM_LEDS);
        FastLED.addLeds<WS2801, name3.DATA_Pin, name3.CLOCK_Pin, BGR>(led, name.NUM_LEDS + name2.NUM_LEDS, name.NUM_LEDS + name2.NUM_LEDS + name3.NUM_LEDS);
        FastLED.addLeds<WS2801, name4.DATA_Pin, name4.CLOCK_Pin, BGR>(led, name.NUM_LEDS + name2.NUM_LEDS + name3.NUM_LEDS, name.NUM_LEDS + name2.NUM_LEDS + name3.NUM_LEDS + name4.NUM_LEDS);
    }

司机

    #include "Led.h"
    #include "MusicChip.h"
    #include "Shapes.h"
    #include "Strip.h"

      const Strip RIGHT(6, 5, 174);
      const Strip MID(4, 3, 200);
      const Strip LEFT(22, 23, 177);
      const Strip FIRE(10, 12, 97);

    void setup()
    {
      delay(1000); // Sanity Check, allows input to settle  
      Serial.begin(9600);
      Led running(RIGHT, MID, LEFT, FIRE);

      MusicChip MAIN(0, 8, 7); 
    }

最佳答案

创建 const 对象的正确语法是:

const Strip RIGHT(6, 5, 174);

请注意,您不需要实际拥有 const 对象来调用采用 const 引用的函数。您可以只传递非 const 对象,并且出于该函数调用的目的,它们将被视为 const。

Yuu 提到了一些关于模板的问题。很难说,因为您没有发布错误消息以及它们发生的位置,但是这里的代码无可救药地被破坏了:

FastLED.addLeds<WS2801, name.DATA_Pin, name.CLOCK_Pin, BGR>(led, name.NUM_LEDS);

和它后面的其他行。模板参数必须在编译时解析。您不能像您所做的那样将变量放在模板参数列表中。

关于c++ - 你能在函数中声明一个常量对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320143/

相关文章:

c++ - sscanf()的问题不会读取char数组中的每个0

ruby-on-rails - Heroku、Twitter api 上的 Rails 未初始化常量错误,在本地工作正常

c - Arduino sketch 的字符串命令有点工作但不是真的

在 Rails 4 中设计 3.0.0rc 自定义属性和强参数

ruby-on-rails-3 - Rails -- 新安装的 gem 的未初始化常量错误

c++ - std::vector<int, std::allocator<char>> 有效吗?

c++ - 如何使用 QRegExp 解析 pacmd 列表输出并查找接收器索引和名称?

c++ - 如何在 C++ 中返回文件名?

c++ - 需要我的输出采用这种形式 XXX.XXX c++

c++ - 带有 SoftWire(I2C 仿真器)库的多个 tcs34725 arduino 颜色传感器