简体   繁体   English

在C ++中将对象矢量的引用作为参数传递

[英]Passing reference of vector of objects as arguments in C++

I'm trying to build a function in which an argument is a reference to a vector of objects. 我正在尝试构建一个函数,其中参数是对对象向量的引用。 In this case name of the object is 'obj', it is an instance of the class 'Example' and it is a vector as defined in vector class. 在这种情况下,对象的名称为'obj',它是类'Example'的实例,并且是向量类中定义的向量。 Object have members like x, y and z. 对象具有成员x,y和z。

The reason I'm trying with passing references is because I want to change the value of obj.z by making use of obj.x and obj.y from inside of the function. 我尝试传递引用的原因是因为我想通过从函数内部使用obj.x和obj.y来更改obj.z的值。

    #include <vector>
    #include <iostream>
    using namespace std;

    class Example{
    public:

        double x;
        double y;
        double z;

        Example()
        : x(0.0),y(0.0){
        }
    };

    void calculate(Example &obj, int M) {
        for(int i = 0; i < M; i++) {
            obj[i].z = obj[i].x * obj[i].y;
        }
    }

    int main() {
        vector<Example> obj;
        int N = 10;
            calculate(obj, N);           
    }

When I run this, I get the following errors: 运行此命令时,出现以下错误:

Inside of the function I have: "Type 'Example' does not provide a subscript operator." 在函数内部,我具有:“类型'Example'不提供下标运算符。” I google'd it and saw that it is related to operator overloading and usage of references. 我用谷歌搜索它,发现它与运算符重载和引用的使用有关。 The solution is probably related to dereferencing my object reference inside of the function, but I wasn't able to manage this one right currently. 该解决方案可能与在函数内部取消引用我的对象引用有关,但是我目前无法正确管理这一对象。

And the second error is outside of the function, inside the main() at the line where I call the function: "No matching function for call to 'calculate'". 第二个错误是在函数外部,在我调用该函数的行的main()内部:“没有匹配的函数可调用'calculate'”。 Here, I assume the error is related to the fact that obj is not just an object but a vector of objects, so I should change the argument somehow. 在这里,我假设错误与以下事实有关:obj不仅是一个对象,而且是对象的向量,因此我应该以某种方式更改参数。 But I haven't been able to correct this one up to now as well. 但是到目前为止,我还无法更正此错误。

So, to summarize, I want to pass a vector of objects to a function as reference, because I want to be able to change a member of the object inside of the function. 因此,总而言之,我想将对象向量传递给函数作为引用,因为我希望能够在函数内部更改对象的成员。

Thank you in advance. 先感谢您。

I want to pass a vector of objects to a function as reference 我想将对象向量传递给函数作为参考

So do that: 这样做:

void calculate(vector<Example> &obj) {
    for(int i = 0; i < obj.size(); i++) {
        obj[i].z = obj[i].x * obj[i].y;
    }
}

int main() {
    vector<Example> obj;
    // put some values into it...

    calculate(obj);
}

I believe you wanted your function to be 我相信你希望你的职能是

void calculate(vector<Example> &obj, int M)

and not 并不是

void calculate(Example &obj, int M)

obj[i] OBJ [I]

'obj' isn't an array. 'obj'不是数组。 You need to declare it as: 您需要将其声明为:

void calculate(Example* obj, int M)

And rather 而是

void calculate(vector<Example>& v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM