これは、このテンプレートのすぐ下に配置された質問とは少し異なります。 2つのテンプレートパラメータを持つコンテナクラスがあるとします。最初の型は型、2番目はコンテナのサイズです。複数のテンプレートパラメータのうちの1つだけを必要とするクラス定義を含むテンプレートコンテナ
ここでは、コンテナの格納サイズが異なる複数のコンテナがあります。本質的には、コンテナの機能(すべての公共のもの、とにかく)は本当に気にするだけですT
; N
は、ローカルストレージを割り当てるためにのみ使用されます(アロケータは、N
で十分でない場合に使用されます)。
私が持っている問題を紹介する簡単な実装例をまとめました。
#include <iostream>
template <typename T, size_t N = 10>
class TestArray
{
public:
T Local[N];
class Iterator
{
public:
T* Array;
int Index;
Iterator() : Array(NULL), Index(-1) { }
Iterator(T* _array, int _index) : Array(_array), Index(_index) { }
bool operator == (const Iterator& _other) const
{
return _other.Index == Index && _other.Array == Array;
}
bool operator != (const Iterator& _other) const
{
return !(*this == _other);
}
template <size_t _N>
Iterator& operator = (const typename TestArray<T, _N>::Iterator &_other)
{
Array = _other.Array;
Index = _other.Index;
return *this;
}
void Next() { ++Index; }
void Prev() { --Index; }
T& Get() { return Array[Index]; }
};
T& operator [] (const int _index) { return Local[_index]; }
Iterator Begin() { return Iterator(Local, 0); }
Iterator End() { return Iterator(Local, N); }
template <size_t _N>
void Copy(const TestArray<T, _N> &_other, int _index, int _count)
{
int i;
for (i = 0; i < _count; i++)
Local[_index + i] = _other.Local[i];
}
};
これは本当に2つの問題です。前に投稿した最初の部分:Template container with multiple template parameters interacting with other template containers with a different template parameter。第二部では、私は次のようにそれを使用しようとしている:ここで
int main() {
TestArray<int> testArray1;
TestArray<int, 25> testArray2;
TestArray<int>::Iterator itr;
itr = testArray1.Begin();
for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
{
itr.Get() = itr1.Index;
}
testArray2.Copy(testArray1, 0, 10);
for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
{
std::cout << itr.Get() << std::endl;
}
return 0;
}
はIDEONEリンクです:http://ideone.com/GlN54
のgcc-4.3.4でコンパイルされたとき、私は以下の取得:
VS2010でprog.cpp: In function ‘int main()’:
prog.cpp:67: error: no match for ‘operator=’ in ‘itr = testArray2.TestArray<T, N>::Begin [with T = int, unsigned int N = 25u]()’
prog.cpp:10: note: candidates are: TestArray<int, 10u>::Iterator& TestArray<int, 10u>::Iterator::operator=(const TestArray<int, 10u>::Iterator&)
prog.cpp:67: error: no match for ‘operator!=’ in ‘itr != testArray2.TestArray<T, N>::End [with T = int, unsigned int N = 25u]()’
prog.cpp:19: note: candidates are: bool TestArray<T, N>::Iterator::operator!=(const TestArray<T, N>::Iterator&) const [with T = int, unsigned int N = 10u]
、私は次を得る:
1>------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1> with
1> [
1> T=int,
1> N=25
1> ]
1> c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(34): could be 'TestArray<T>::Iterator &TestArray<T>::Iterator::operator =(const TestArray<T>::Iterator &)'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1> with
1> [
1> T=int
1> ]
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1> with
1> [
1> T=int,
1> N=25
1> ]
1> c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(19): could be 'bool TestArray<T>::Iterator::operator !=(const TestArray<T>::Iterator &) const'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1> with
1> [
1> T=int
1> ]
私はIterator& operator =
WOUを考えましたこの代入演算子は動作するはずですが、明らかにそうではありません。多分私は太っているかもしれませんが、私はここで正しい解決方法を決定することに失敗しています。誰にも何か提案はありますか?
Big Threeが必要です!あなたはデフォルトコンストラクタと代入オーバーロードを持っていますが、Iteratorにコピーコンストラクタはありません! –
@Bob彼はデフォルトのコピーコンストラクタを使用しますか? –
@VJo、Big Threeが重要であることに留意して、それは問題の答えではありません。このインスタンスのデフォルトのコピーコンストラクタは問題ありませんが、Big Threeの法則に従うことは常に良い習慣です。 –