c++ - 将 vector (在结构中定义时)传递给函数

标签 c++ struct

当我尝试运行它时,出现错误:

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.  

我如何访问函数中的 vector 参数?

我是不是传错了?

请注意,我需要保留函数头。

此外,有没有一种方法可以让 Args 中的 vector args 保留为未定义的大小,并在创建 Uniform 时定义其大小?

感谢您的任何反馈。

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;
}

最佳答案

我想你想要这个:

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

因为 argsArgs 类型并且它有一个成员 args 是一个 vector 。我建议您适本地命名您的结构和变量以避免混淆。

关于c++ - 将 vector (在结构中定义时)传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880693/

相关文章:

c++ - opengl - 镜像着色器的可能性?

c++ - 如何在函数声明中模板化容器类型?

c++ - 在 "wrapper"类中使用原子类型

c++ - vector::erase() 也删除结构的成员 vector

go - Nocopy 最小的例子?

c - 如何通过指针将结构元素结构传递给函数

c++ - BOOST_FUSION_ADAPT_STRUCT 没有采用正确数量的参数

c++ - 如何索引实体中基本组件列表中的所有派生组件

c++ - 我怎么知道是什么导致错误 : arithmetic on a pointer to an incomplete type 的不完整类型不完整

c - 关于C中返回函数的问题