プロジェクトのダウンタイム中に C++ のブラッシュアップに取り組んでおり、そのために Linked List プロジェクトを作成しました。このプロジェクトでは、設定されたインデックスで現在の値を返したいと思います。これを行う方法は既にありますが、演算子のオーバーロードを使用して解決したいと考えています。
そのために、ブラッシュアップするために必要な調査を行い、次のコードを作成しました (すべての人に私のコードを適用するのではなく、関連するセクションのみを貼り付けます)。
T& operator[](const int index);
template<class T>
T& LinkedList<T>::operator[](const int index)
{
try
{
if(!isIndexValid(index)) throw ior;
Node<T> *temp = _head;
for(int i=1; i<=index; i++)
{
temp = temp->Next;
}
return temp->Value;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
私の主な機能には、次の行があります。
int foo = list[5];
私にはすべて問題ないように見えますが、コンパイルすると次のエラーが発生します。
error C2440: 'initializing' : cannot convert from 'LinkedList<T>' to 'int'
これを修正する方法はありますか?
ありがとうございました!
質問された方のために、 Node クラスの定義方法を次に示します。
template<class T>
class Node
{
public:
Node();
Node(const T value);
T Value;
Node<T> *Prev;
Node<T> *Next;
};
そして、これが私のリスト変数の宣言です:
LinkedList<int> *list = new LinkedList<int>();