テンプレート化されたクラスで[int]演算子をオーバーロードしようとしていますが、常にC2676エラーが発生し、その後にVisual StudioE0349が続きます。「これらのオペランドに一致する演算子[]はありませんWMSTR [int]」
私のテンプレートクラス:
template <typename T, unsigned int N>
class MyString{
public:
// ... Non relevant tested code
template<typename T, unsigned int N>
T& operator[](int index) {
// Assert index size
SLOW_ASSERT(index >= 0 && index < N);
// Return current value
return m_buffer[index];
}
template<typename T, unsigned int N>
const T& operator[](int index) const {
// Assert index size
SLOW_ASSERT(index >= 0 && index < N);
// Return current value
return m_buffer[index];
}
private:
T m_buffer[N];
}
SLOW_ASSERT(...)はassert(...)の単なるラッパーです
メインの内容:
#include "path_to_template.h"
typedef MyString<wchar_t, 24> WMSTR;
int main(void){
WMSTR str = L"Test";
str[0] = L'X'; // <-- Error here
return 0;
}
両方operator[]
を関数テンプレートとして宣言しているので、それらのテンプレートパラメータを推測できず、呼び出しが失敗します。
それらを非テンプレートにすることはうまくいくはずです。私はあなただけのテンプレートパラメータを参照したいとT
し、N
クラステンプレートのMyString
の中でoperator[]
。
T& operator[](int index) {
// Assert index size
SLOW_ASSERT(index >= 0 && index < N);
// Return current value
return m_buffer[index];
}
const T& operator[](int index) const {
// Assert index size
SLOW_ASSERT(index >= 0 && index < N);
// Return current value
return m_buffer[index];
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加