小编bor*_*but的帖子

没有这样的运算符"[]"匹配这些操作数

我正在尝试制作一个程序来演示我的CS类使用模板和重载操作符.这是相关代码:

主要:

    ArrayTemplate<int> number(0);
            number[0] = 1;
            number[1] = 2;
            number[2] = 3;

    ArrayTemplate<string> word("na");
            word[0] = "One";
            word[1] = "Two";
            word[2] = "Three";
Run Code Online (Sandbox Code Playgroud)

标题:

template<class T>
T& operator [](const int index) 
{ 
    if(index >= 0 && index < ARRAY_MAX_SIZE)
        return items[index];
    else
    {
        cerr << "INDEX OUT OF BOUNDS!!!";
        exit(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试使用我的重载下标运算符时,我得到标题中显示的错误消息:"没有这样的运算符"[]"匹配这些操作数"我不完全确定原因.它为我的整数数组和我的字符串数组都做了.任何帮助表示赞赏.

c++ templates operator-overloading

0
推荐指数
1
解决办法
4052
查看次数

标签 统计

c++ ×1

operator-overloading ×1

templates ×1