C-错误:预期为'''';或';)';在'之前=';代币

C-错误:预期为'''';或';)';在'之前=';代币,c,C,我需要帮助。它在“=”标记之前显示“错误:应为“;”、“、”或“)” 在第5行(getRandom方法),当我尝试构建并运行它时。我已经一遍又一遍地看了好几遍,但我似乎不知道问题出在哪里 #include <pthread.h> #include <stdio.h> #include <stdlib.h> double getRandom (double min = -1, double max = 1) { return min + (rand()

我需要帮助。它在“=”标记之前显示“错误:应为“;”、“、”或“)” 在第5行(getRandom方法),当我尝试构建并运行它时。我已经一遍又一遍地看了好几遍,但我似乎不知道问题出在哪里

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

double getRandom (double min = -1, double max = 1) {
    return min + (rand() * (max - min) / RAND_MAX);
}

int totalPoints = 0;
int pointsInCircle = 0;

void *countPoints (void *X) {
    for (int i = 0; i < totalPoints; i++) {
        double X = getRandom();
        double Y = getRandom();
        if (X*X + Y*Y <= 1)pointsInCircle++;
    }
    return NULL;
}


int main()
{
    srand(time(NULL));
    pthread_t thread;
    printf ("Enter total points for experiment : ");
    scanf  ("%d" , totalPoints);
    pthread_create(&thread, NULL, &countPoints, NULL);
    pthread_join(thread, NULL);

    double PI = (4.0 * pointsInCircle) / totalPoints;
    printf ("Approximate value for PI for total points %d is: %d " , totalPoints, PI);



    return 0;
}
#包括
#包括
#包括
double getRandom(双最小值=-1,双最大值=1){
返回最小值+(rand()*(max-min)/rand\u max);
}
int totalPoints=0;
int pointsInCircle=0;
void*countPoints(void*X){
对于(int i=0;i
double getRandom (double min, double max) {
    return min + (rand() * (max - min) / RAND_MAX);
}
当您在代码中没有实际参数的情况下调用它时

    double X = getRandom();
    double Y = getRandom();
您应该将这些呼叫替换为

    double X = getRandom(-1, 1);
    double Y = getRandom(-1, 1);
这不是C

double getRandom (double min, double max) {
    return min + (rand() * (max - min) / RAND_MAX);
}
当您在代码中没有实际参数的情况下调用它时

    double X = getRandom();
    double Y = getRandom();
您应该将这些呼叫替换为

    double X = getRandom(-1, 1);
    double Y = getRandom(-1, 1);

这个错误也会包含在它遇到的错误的行号上,它不把默认值作为函数参数,你是在编程C还是C++?你做的是C++中有效的,而不是在C.,你在哪里得到那个错误?在哪条线上?错误也会是当它遇到了Error时,它的行号不把默认值作为函数参数,你是在编程C还是C++?你做的是C++中有效的,而不是C中的。我已经做了一些修改,但是当我试着运行时,它显示了“错误:函数的‘Grand’的参数太少了。”第14行。我该怎么办?@Kamal是的,在C中没有默认的实际参数。每次调用函数时,您都需要传递实际参数。@Kamal如果您想使用-1和1调用函数,请执行
getRandom(-1,1);
我已经做了更改,但当我尝试运行时,它显示“错误:函数'getRandom'的参数太少”在第14行。我该怎么办?@Kamal是的,在C中没有默认的实际参数。每次调用函数都需要传递实际参数。@Kamal如果要使用-1和1调用函数,请执行
getRandom(-1,1);