C++ 重载类下标运算符以访问成员std::vector对象的元素
我正在解析一个基于文本的文件以从中读取变量。文件中是否存在变量很重要,因此我决定编写一个模板类,该类将保存变量的值(C++ 重载类下标运算符以访问成员std::vector对象的元素,c++,c++11,operator-overloading,stdvector,subscript-operator,C++,C++11,Operator Overloading,Stdvector,Subscript Operator,我正在解析一个基于文本的文件以从中读取变量。文件中是否存在变量很重要,因此我决定编写一个模板类,该类将保存变量的值(value)及其存在标志(Exists) 我收到以下错误消息: 错误C2679二进制' 那些返回类型是错误的;您返回的是包含的类型,而不是容器类型。您可以为此使用decltype: auto operator[](size_t Index) const -> decltype(Value[Index]) { return Value[Index]; } auto
value
)及其存在标志(Exists
)
我收到以下错误消息:
错误C2679二进制'
那些返回类型是错误的;您返回的是包含的类型,而不是容器类型。您可以为此使用decltype
:
auto operator[](size_t Index) const -> decltype(Value[Index])
{
return Value[Index];
}
auto operator[](size_t Index) -> decltype(Value[Index])
{
return Value[Index];
}
您返回了错误的类型
对于常量类型和运算符[](大小索引)常量
,类型
是std::vector
,这意味着您试图返回的是向量
,而不是向量
的元素
尝试将返回值的类型更改为typename type::value\u type
,例如
const typename Type::value_type& operator[](size_t Index) const
您的运算符重载已声明
const Type & operator[](size_t Index) const
但AVector被宣布为
const MyVariable<std::vector<int>>
const MyVariable
因此,在您的例子中,Type
是std::vector,没有,只有当包含的变量类型(即;ValueType
)是std::vector
时,答案才是正确的。如果我们试图存储基本数据类型(例如;int
或float
),编译器(MSVC14)会给出以下错误,因为里面不会有任何隐式的下标运算符运算符[]
或值类型
成员类型定义
'InputFileVariable<bool,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<int,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<uintmax_t,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<float,std::string>::value_type': is not a type name, static, or enumerator
输出:
Size = 4
MyVar1[0] = 2
MyVar1[1] = 6
MyVar1[2] = 4
MyVar1[3] = 8
MyVar2 = 3.14
您可以查看可选
(不要求类型
是默认可构造的)。非常感谢。我应该分别将decltype(Value[Index])
更改为const decltype(Value[Index])&
和decltype(Value[Index])&
吗?@hkBattousai Nah,在这种情况下,decltype
s将按照您的要求工作。
const MyVariable<std::vector<int>>
'InputFileVariable<bool,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<int,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<uintmax_t,std::string>::value_type': is not a type name, static, or enumerator
'InputFileVariable<float,std::string>::value_type': is not a type name, static, or enumerator
#include <vector>
#include <string>
template<class ValueType, class KeyType = std::string>
class InputFileVariable
{
public:
const KeyType Key;
ValueType Value;
bool Exists;
InputFileVariable(KeyType && Key, ValueType && Value, bool Existance = false)
: Key (std::forward<KeyType> (Key)),
Value (std::forward<ValueType>(Value)),
Exists (Existance)
{
}
size_t size() const
{
return Value.size();
}
const InputFileVariable & operator=(InputFileVariable && Another)
{
Key = std::forward<InputFileVariable>(Another).Key;
Value = std::forward<InputFileVariable>(Another).Value;
Exists = true;
return *this;
}
template <class ElementType = ValueType::value_type>
const typename ElementType & operator[](size_t Index) const
{
return Value[Index];
}
template <class ElementType = ValueType::value_type>
typename ElementType & operator[](size_t Index)
{
return Value[Index];
}
operator const ValueType & () const
{
return Value;
}
operator ValueType & ()
{
return Value;
}
};
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
// Used with "std::vector":
InputFileVariable<std::vector<int>> MyVar1("MV1", {2, 4, 6, 8}, true);
const size_t SIZE = MyVar1.size();
std::cout << "Size = " << SIZE << std::endl;
int Temp = MyVar1[1];
MyVar1[1] = MyVar1[2]; // Here we call both the const and non-const operators.
MyVar1[2] = Temp;
for (size_t i=0; i<SIZE; i++)
{
std::cout << "MyVar1[" << i << "] = " << MyVar1[i] << std::endl;
}
// Used with "double":
InputFileVariable<double> MyVar2("MV2", 3.14, true);
std::cout << std::endl << "MyVar2 = " << MyVar2 << std::endl;
std::cout << std::endl;
_wsystem(L"timeout /t 60 /nobreak");
return 0;
}
Size = 4
MyVar1[0] = 2
MyVar1[1] = 6
MyVar1[2] = 4
MyVar1[3] = 8
MyVar2 = 3.14