繁体   English   中英

sqrt 函数导致“'void*' 不是指向对象类型的指针”错误

[英]sqrt function causing " 'void*' is not a pointer-to-object type" error

我在 C++ 中工作,制作了一个非常简单的函数,用于计算用户输入的给定数字的标准偏差。 但是当我尝试将sqrt函数与我的变量mean一起使用时,在尝试编译我的程序时出现错误:

'void*' 不是指向对象的类型

下面是我的代码,以及错误信息的图片。

#include <iostream>
#include <tgmath.h>
#include "stddev.h" //this is just a seperate header file that I have going with my program

using namespace std;

void stats (float *data, int n){

    float mean = 0;
    int i;

    for (i = 0; i < n; i++){
        mean += data[i];
    }

    mean = mean / n;

    for(i = 0; i < n; i++) { //subtracting the mean from the number and squaring the result.
        data[i] = data[i] - mean;
        data[i] = data[i] * data[i];
    }

    mean = 0;

    for (i = 0; i < n; i++){
        mean += data[i];
    }

    mean = mean / n;

    float stdDeviation = sqrt(mean); //Here is where I get the error

    cout << "\t Your Standard Deviation is : " << stdDeviation << endl;
}

图片

在 C++ 中不推荐使用<tgmath.h> (或<ctgmath> )。

它应该仍然有效 - <tgmath.h>意味着完全等同于包括<cmath><complex> - 所以你正在查看编译器/库错误。 但也许这是供应商并不真正有兴趣花时间修复的事情,因为它已被弃用。

使用#include <cmath>而不是#include <tgmath.h>事情应该会更顺利。

暂无
暂无

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

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