Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;函数内部错误:表达式必须具有指向对象类型的指针 ./P> double RandomVec(std::vector<double>& vec, const int cols) { //... for (int element = 0; element < cols + 1; element++) { vec.push_back(dis(gen)); } } int main { // ... std::vector<double> my_vector; // ... } 双随机向量(std::vector&vec,const int cols){ //... for(int元素=0;元素_C++ - Fatal编程技术网

C++ C++;函数内部错误:表达式必须具有指向对象类型的指针 ./P> double RandomVec(std::vector<double>& vec, const int cols) { //... for (int element = 0; element < cols + 1; element++) { vec.push_back(dis(gen)); } } int main { // ... std::vector<double> my_vector; // ... } 双随机向量(std::vector&vec,const int cols){ //... for(int元素=0;元素

C++ C++;函数内部错误:表达式必须具有指向对象类型的指针 ./P> double RandomVec(std::vector<double>& vec, const int cols) { //... for (int element = 0; element < cols + 1; element++) { vec.push_back(dis(gen)); } } int main { // ... std::vector<double> my_vector; // ... } 双随机向量(std::vector&vec,const int cols){ //... for(int元素=0;元素,c++,C++,我不明白我做错了什么,有人能告诉我这个错误是关于什么的,我如何解决它吗?谢谢 #include <iostream> #include <random> using namespace std; double RandomVec(double W, const int cols) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<

我不明白我做错了什么,有人能告诉我这个错误是关于什么的,我如何解决它吗?谢谢

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

double RandomVec(double W, const int cols) {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);

    for (int element = 0; element < cols +1; element++) {
        W[element] = dis(gen);
    }
return W;
}

int main()
{
    const int cols = 4;

    double RV;
    double W;
    RV = RandomVec (W,cols);

    return 0;
}
#包括
#包括
使用名称空间std;
双随机向量(双W,常数int cols){
std::随机_装置rd;
标准:mt19937 gen(rd());
一致实分布dis(0,1);
for(int元素=0;元素
你自己也说过,你想填充一个数组。如果你看一下你的
W
定义,它是一个单
double
,而不是一个数组。

当你说
W[element]
时,你试图将一个普通的老
double
作为一个double数组来访问。您必须在
RandomVec(…)
外部或内部创建一个双精度数组并返回它

如果您想传递一个数组,请考虑在<代码>主< <代码>中使用<代码> STD::vector < /代码>,并将向量引用到<代码> RandomVec < /代码> ./P>

double RandomVec(std::vector<double>& vec, const int cols) {
  //...
  for (int element = 0; element < cols + 1; element++) {
    vec.push_back(dis(gen));
  }
}

int main {
  // ...
  std::vector<double> my_vector;
  // ...
}
双随机向量(std::vector&vec,const int cols){ //... for(int元素=0;元素下面是一个使用向量的可能解决方案。如果需要一个包含4列的向量,则索引必须从0到
,而不是
列+1
。如果选择使用
std::
,则无需使用命名空间std添加
在顶部。在末尾添加生成的随机向量的打印输出

#include <iostream>
#include <conio.h>
#include <random>


std::vector<double> RandomVector(std::vector<double>& vector, const int columns) {

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);

    for (int element = 0; element < columns; element++)
        vector.push_back(dis(gen));

    return vector;
}



int main() {

    const int columns = 4;
    std::vector<double> my_vector;

    my_vector = RandomVector(my_vector, columns);

    // print out generated random vector
    for (std::vector<double>::const_iterator i = my_vector.begin(); i != my_vector.end(); ++i)
        std::cout << *i << ' ';

    // wait for a character to exit
    _getch();

    return 0;
}
#包括
#包括
#包括
std::vector RandomVector(std::vector&vector,常量int列){
std::随机_装置rd;
标准:mt19937 gen(rd());
一致实分布dis(0,1);
for(int-element=0;elementstd::cout This:
double W
是double。This:
double W[]
是一个double数组。是什么让你真的认为你可以将索引
操作符[]
double
类型一起使用?谁教你的?是的,但我首先尝试将它声明为
double W[cols+1];
因此它的大小应该是正确的,但是我得到了另一个错误,即“double类型的参数与double类型的参数不兼容”,同时保留了我的原始错误..函数希望我如何创建W?@petahunks
double W[cols+1] VLA不是标准的C++兼容的语法。使用<代码> STD::向量W(COLSS + 1);