没有操作员" >> "匹配这些操作数

时间:2015-12-15 23:23:58

标签: c++ visual-studio

我正在编写一个函数定义,让用户输入数组的元素但得到错误为cin >> a[index];

void show(const double a[], unsigned els) {
    size_t index = 0;

    for (index = 0; index < els; index++) {
        cin >> a[index];
        }

    for (index = 0; index < els; index++) {
        if (a[index] == a[0]) {
            cout << "{" << a[index] << ",";
        }
        else if (a[index] == a[els - 1]) {
            cout << a[index] << "}";
        }
        else { cout << a[index] << ","; }
    }
    cout << endl;
}

2 个答案:

答案 0 :(得分:7)

在复制的代码中查看我的评论:

use Youtube;

public function search(Request $request)
{
    Youtube::parseVidFromURL($request->input('url'));
}

答案 1 :(得分:1)

请记住,使用[]运算符传递参数C ++会产生误导;实际上,这个运算符是在指针上定义的。那么函数show()中真正发生的是接收传递的数组的第一个元素的指针。将此指针作为const传递,您无法更改指针指向的值,您只能应用指针的aritmetic规则(例如++,赋值,减法......)。 可能的解决方案可能是:

void show(double *a, unsigned els) {
    size_t index = 0;

    for (index = 0; index < els; index++) {
        cin >> a[index]; //that is the same of --> cin >> *(a + index)
        }

    for (index = 0; index < els; index++) {
        if (a[index] == a[0]) {
            cout << "{" << a[index] << ",";
        }
        else if (a[index] == a[els - 1]) {
            cout << a[index] << "}";
        }
        else { cout << a[index] << ","; }
    }
    cout << endl;
}

我测试了这段代码并且它有效。因此,>>个操作数存在运算符double

<强>优化: 请注意,如果要创建通用编程函数,则应将容器的第一个和最后一个元素的指针(尽管元素的数量为els)传递给函数(数组,列表,向量... )并以这种方式扫描容器:

while (first != last) {
        //your code ...
}

这与STL的创造者Alxander Stepanov的风格相同。

相关问题