Jack Jack - 1 year ago 72
C++ Question

pass a vector (when defined in a struct) to a function

When I try and run this, I get the errors:

error C2676: binary '[': 'const Args' does not define this operator or a conversion to a type acceptable to the predefined operator.

error C2088: '[': illegal for struct.

error C2660: 'Mergesort::get_uniform': function does not take 1 arguments.

How would I access the vectors args in my function?

Am I passing it incorrectly?

Note that I need to leave the function header as is.

Also, is there a way that I can leave the vector args in Args as an undefined size and define its size when creating Uniform?

Thank you for any feedback.

int get_sequence(double S[], int k, const Args &args); // function prototype

struct Args
string name;
vector<double> args;
Args() : args(2) {};

int main()

Args uniform;
uniform.name = dist_name;
uniform.args[0] = min;
uniform.args[1] = max;

distribution.get_sequence(S, k, uniform);

} // end main

int Mergesort::get_sequence(double S[], int k, const Args &args)

for (int i = 0; i < k; i++)
S[i] = get_uniform(args[0], args[1]);
cout << S[i] << " ";

return k;

Answer Source

I think you want this instead:

S[i] = get_uniform(args.args[0], args.args[1]);

Because args is of type Args and it has a member args which is a vector. I'd suggest you name your structs and variables appropriately to avoid confusion.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download