簡體   English   中英

類型不匹配的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM