2016-11-01 8 views
-1

私はカスタムクラスのオブジェクトの配列をbcLEDにしようとしていますが、エラーが発生しています。 エラー: 'operator ='に一致しません(オペランドの種類は 'bcLed'と 'bcLed *')C++だから、エラー: 'operator ='に一致しない理由

何故私にその理由を教えてもらえますか?私はそれが単純なものになることを知っています。

なぜ私はここにC++で不特定の長さの配列を作成し、それにオブジェクトを追加するたびに新しい行を追加する方法があるのですか?

void PopulateLEDS(){ 
    int i; 
    bcLed ledArr[17]; 
    for (i = 0; i< 16; i++) 
    { 
     ledArr[i] = new bcLed(); 
     ledArr[i].id = i; 
     ledArr[i].charge = 0; 
    } 
} 

OKだから私は避け、私はC++の構文でアップトリップい場所を確認するようにコードの本体を貼り付けるつもりです1万記事を作成するための

より多くの助けを必要としています。

lattestエラーが

/Users/bencawley/Documents/Arduino/Test/Bens_Lights/Bens_Lights.ino: In function 'void PopulateLEDS()': 
Bens_Lights:49: error: expected primary-expression before 'public' 
    public:bcLed ledArr[17]; 
    ^
Bens_Lights:52: error: 'ledArr' was not declared in this scope 
     ledArr[i].id = i; 
     ^
/Users/bencawley/Documents/Arduino/Test/Bens_Lights/Bens_Lights.ino: In function 'void BensPattern(uint8_t)': 
Bens_Lights:69: error: 'ledArr' was not declared in this scope 
     strip.setPixelColor(i,0, 0, ledArr[i].charge, 0); 
           ^
Using library Adafruit_NeoPixel at version 1.0.6 in folder: /Users/bencawley/Documents/Arduino/libraries/Adafruit_NeoPixel 
exit status 1 
expected primary-expression before 'public' 

をされ、私のコードは次のとおりです。

class bcLed{ 
    public:int id; 
    public:int charge; 

    void incCharge(int amt) 
    { 
     charge = charge+amt; 
     if(charge >= 255){charge = 255;} 
    } 
}; 

void setup() { 
    strip.begin(); 
    strip.show(); // Initialize all pixels to 'off' 

    PopulateLEDS(); 
} 

void loop() { 
    // Some example procedures showing how to display to the pixels: 
    BensPattern(45); 

} 

void PopulateLEDS(){ 
    int i; 
    bcLed ledArr[17]; 
    for (i = 0; i< 17; i++) 
    { 
     ledArr[i].id = i; 
     ledArr[i].charge = 0; 
    } 
} 

void BensPattern(uint8_t wait) 
{ 
    uint16_t i, j; 
    int rn = rand() % strip.numPixels() ; 

    for (i = 0; i<strip.numPixels(); i++) 
    { 
     strip.setPixelColor(i,0, 0, 0, 0); 
    } 

    for (i = 0; i<rn; i++) 
    { 
     strip.setPixelColor(i,0, 0, ledArr[i].charge, 0); 
     ledArr[i].incCharge(1); 
    } 

     strip.show(); 
     delay(wait); 
} 
+5

あなたはJavaの背景から来ていますか? – StoryTeller

+1

17のbcLed配列があります。あなたは 'bcLed * 'を割り当てようとしています。 'bcLed *'の右辺を取る 'bcLed'の' operator = 'オーバーロードを定義していない限り、それはうまくいきません(とにかくそれを望んでいないでしょう)。あなたはその割り当てステートメント*は一切必要としません。あなたはすでに17の 'bcLed'オブジェクトを持っています。 – WhozCraig

+0

'ledArr [i]'は 'bcLed'です。 'new bcLed()'は 'bcLed *'を返します。 – zvone

答えて

3

newは、C++では必ずしも必要ではなく、間違いなくここにはありません。 newは、自動割り当てが十分でない場合に動的メモリを割り当てます。変数の有効範囲を超えて使用する場合は、通常はnewを使用します。 newで割り当てられたメモリは、メモリリークを回避するために、常にdelete dでなければなりません。現代のC++では、スマートポインタがあるので、newの使用はあまり必要ありません。

bcLed ledArr[17]; 

これはすでに、(あなたがC#でnewを使用する方法のように、何のクリーンアップを必要としません)あなたのためにそれらの上にnewを使用する必要が17のbcLed Sを作成しません。それらと一緒に作業してください。あなたのループ状態も間違っています、それは< 17であるはずです。

for (i = 0; i < 17; i++) 
{ 
    ledArr[i].id = i; 
    ledArr[i].charge = 0; 
} 

also why i am here is there a way to create an array of an unspecified length in C++ and then just append it with an new row each time I want to add an object to it?

はい、それはstd::vectorが何のためにあるのかだ :あなたがあなたの代わりに範囲ベースのループを使用して使用することができますC++ 11へのアクセス権を持っている場合は

#include <vector> 
std::vector<bcLed> ledArr(17); 

//loop over them: 
for(int i = 0; i < ledArr.size(); ++i) 
{ 
    //ledArr[i] 
} 

//or: 
for(std::vector<bcLed>::iterator itr = ledArr.begin() itr != ledArr.end(); ++itr) 
{ 
    //*itr 
} 

// to insert to the back of the vector use push_back: 

bcLed aLed; 
ledArr.push_back(aLed); 

emplace_back

#include <vector> 
std::vector<bcLed> ledArr(17); 

//loop over them, just to iterate: 
for(const auto& led : ledArr) 
{ 
    //led.id 
    //led.charge 
} 

//appending to the vector: 

ledArr.emplace_back(/*constructor arguments*/); 

Toあなたのコメントにお答えください

ok im going to brave and ask this when you say "if you want the variable to outlive it's scope or you're working with low level memory" I don't understand what any of that means... well mostly I don't understand what you mean by scope or low level memory. Could you explain those? is scope the time that the method runs for?

変数のスコープは、それが定義されているコンテキストです。自動ストレージは、その範囲の終わりまで存続します。かっこ{ }はスコープを示します。たとえば、

void foo() 
{ 
    int x; 
    bcLed aLed; 
    { //create a new inner scope 
    bcLed innerLed; 
    } //scope ends, all automatic variables are destroyed (innerLed in this case) 

    //can't use `innerLed` here. 

    int new_int = x; 
} // scope ends, same goes, new_int, x, aLed are destroyed. 

実際には、良い本で差異や使用時期を教えてくれます。

+0

また、 'emplace_back'を使ってC++ 11でインプレイスの要素を構築することも考えてください。 – Asu

+1

@Asu少しC++ 11のグッズを追加。 –

+0

私は勇気を出して、 "あなたが変数の有効期間を長くしたい、または低レベルのメモリを使って作業したいと思ったら、これを尋ねるときにこれを尋ねます。私はそれが何を意味するのか分かりません。スコープや低レベルのメモリの意味を理解する。あなたはそれらを説明できますか?スコープはメソッドの実行時間ですか? – skyzzle

1

ledArr[i] = new bcLed();エラーメッセージが言ったように、あなたがbcLedへのポインタを割り当てることができない、動作しない(つまり、 bcLed*)からbcLedまでである。

bcLed ledArr[17];については、配列の17要素がデフォルトで構築されています。 newは全く必要ありません。だから、エラーを引き起こすコードを削除するだけで、次のコードは正常に動作します。 std::vectorを行うことになって何

bcLed ledArr[17]; 
for (i = 0; i < 16; i++) 
{ 
    ledArr[i].id = i; 
    ledArr[i].charge = 0; 
} 

is there a way to create an array of an unspecified length in C++ and then just append it with an new row each time I want to add an object to it?


あなたは、配列のすべての要素を処理したい場合は、forの条件はi < 17でなければなりません。

関連する問題