C++ Arduino Vector 类型不匹配的问题?

C++ Arduino Vector issues with mismatched types?

我一直在尝试为通过 Arduino Uno R3 控制的一条 RGB LED 编写一些效果。对于上下文,我正在制作一个球(一组 5 个 LED)的动画,它沿着条带前后移动,不时弹跳。

导致问题的矢量是 Colour 对象,我想将其作为 CRGB 颜色的矢量,当传递给函数的可用颜色数量少于可用颜色数量时,它只使用第一个 x数量,但如果球的数量多于颜色,那么它应该简单地重新使用它们。

如果我的格式等有任何错误,我们深表歉意。

问题是当我尝试将向量作为构造函数传递给 BouncingBallEffect 时出现以下错误:

[File-Path]\sketch_jul20b\sketch_jul20b.ino: In constructor 'BouncingBallEffect::BouncingBallEffect(size_t, size_t, byte, bool)':
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<CRGB>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'CRGB [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = CRGB]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<CRGB>::Vector(const Vector<CRGB>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<CRGB>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<CRGB>::Vector(Vector<CRGB>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<CRGB>&&'

我的代码在这里:

#include <Arduino.h>
#define FASTLED_INTERNAL
#include <FastLED.h>

using namespace std;
#include <Vector.h>

static const CRGB ballColours [] = {
    CRGB::Green,
    CRGB::Red,
    CRGB::Blue,
    CRGB::Orange,
    CRGB::Indigo
};

class BouncingBallEffect {
  private:

    double InitialBallSpeed(double height) const {
        return sqrt(-2 * Gravity * height);         // Because MATH!
    }

    size_t  _cLength;           
    size_t  _cBalls;
    byte    _fadeRate;
    bool    _bMirrored;

    const double Gravity = -9.81;                   // Because PHYSICS!
    const double StartHeight = 1;                   // Drop balls from max height initially

    Vector<double> ClockTimeAtLastBounce, Height, BallSpeed, Dampening;
    Vector<CRGB>   Colours;

  public:

    // BouncingBallEffect
    //
    // Caller specs strip length, number of balls, persistence level (255 is least), and whether the
    // balls should be drawn mirrored from each side.

    BouncingBallEffect(size_t cLength, size_t ballCount = 3, byte fade = 0, bool bMirrored = false)
        : _cLength(cLength - 1),
          _cBalls(ballCount),
          _fadeRate(fade),
          _bMirrored(bMirrored),
          ClockTimeAtLastBounce(ballCount),
          Height(ballCount),
          BallSpeed(ballCount),
          Dampening(ballCount),
          Colours(ballCount)
    {
        for (size_t i = 0; i < ballCount; i++) {
            Height[i]                = StartHeight;                 // Current Ball Height
            Dampening[i]             = 0.90 - i / pow(_cBalls, 2);  // Bounciness of this ball
            BallSpeed[i]             = InitialBallSpeed(Height[i]); // Don't dampen initial launch
        }
    }
};

任何人都可以提供任何帮助,我们将不胜感激,谢谢。

Arduino 的 Vector 库 (https://github.com/janelia-arduino/Vector) 在构建时接受一个静态分配的数组,不像 std::vector,你可以将 size_t 传递给它。

template <typename T>
class Vector
{
public:
  Vector();
  template <size_t MAX_SIZE>
  Vector(T (&values)[MAX_SIZE],
    size_t size=0);
  // ...
};

您将需要传递数组而不是 size_t,或者在这些成员初始值设定项期间使用不同的数据结构:

      // ...
      ClockTimeAtLastBounce(ballCount),
      Height(ballCount),
      BallSpeed(ballCount),
      Dampening(ballCount),
      Colours(ballCount)