Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载类下标运算符以访问成员std::vector对象的元素_C++_C++11_Operator Overloading_Stdvector_Subscript Operator - Fatal编程技术网

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