operator<<
サイズが不明な配列の内容を出力するためにオーバーロードするのに問題があります。私は解決策を探しましたが、私が見つけた唯一の方法は、すべてのプライベート データ メンバーを構造体に入れる必要があるというものでした (これは私には少し不必要に思えます)。関数を編集してフレンドにしたり、 (または const)に変更*q
したりすることはできません。&q
これが私の << オーバーロード コードです。
ostream& operator<<(ostream& out, Quack *q)
{
if (q->itemCount() == 0)
out << endl << "quack: empty" << endl << endl;
else
{
int i;
int foo;
for (int i = 0; i < q->itemCount(); i++ )
{
foo = (*q)[i];
out << *(q + i);
} // end for
out << endl;
}
return out;
}
そして、ここに私のプライベートデータメンバーがあります:
private:
int *items; // pointer to storage for the circular array.
// Each item in the array is an int.
int count;
int maxSize;
int front;
int back;
関数の呼び出し方法は次のとおりです (これは編集できません)。
quack = new Quack(QUACK_SIZE);
//put a few items into the stack
cout << quack;
出力のフォーマット方法は次のとおりです。
quack: 1, 2, 3, 8, 6, 7, 0
配列が空の場合、
quack: empty
どんな助けでも大歓迎です。ありがとうございました!