无效使用void表达式 - C ++

时间:2015-10-09 15:20:31

标签: c++ c++11 compiler-errors system-calls

问题是由于这个错误我无法编译:

ejercicio_4.cpp: In function ‘int main()’: *
ejercicio_4.cpp:32:49: error: cannot call member function ‘void Calculos::calcular_desvtipica(double)’ without object
  P[2] = thread(Calculos::calcular_desvtipica(5.0), p3);

这是 Calculos.h

#ifndef CALCULOS_H
#define CALCULOS_H
#include <iostream>
#include <string>
#include <chrono>
#define N 100
using namespace std;

class Calculos {
public:
    Calculos(double T[], int op);  
    Calculos(); 
    void run();
    void calcular_desvtipica(double media);

    double *T;
    int op;
    double max, min, desvtipica, media;
};
#endif

Calculos.cpp

#include <iostream>
#include <string>
#include <chrono>
#include "Calculos.h"
using namespace std;

Calculos::Calculos(double T[], int op) { 
    this->T = T;
    this->op = op;
};

Calculos::Calculos() { }

void Calculos::run() {
    if(op == 1) {   //calcular media
        double suma = 0.0;
        for(int i = 0; i < N; i++) {
            suma = suma + T[i];
        }
        media = (double)(suma/N);
    }
    else if(op == 2) {  //calcular maximo y minimo
        double datoMax = T[0], datoMin = T[0];
        for(int i = 1; i < N; i++) {
            if(T[i] > max) max = T[i];
            if(T[i] < min) min = T[i];
        }
        max = datoMax;
        min = datoMin;
    }
    else {  //op=3 calcular desviacion tipica
        cout << "que cojones hago aqui" << endl;
    }
};

void Calculos::calcular_desvtipica(double media) {
    desvtipica = media/2.0;
};

以下是主要方法

#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include "Calculos.h"
using namespace std;

int main() {
    double T[N];
    for(int i = 0 ; i < N; i++) {
        T[i] = (double)rand();
    }

    thread P[3];

    Calculos p1(T, 1),
             p2(T, 2),
             p3(T, 3);
    P[0] = thread(&Calculos::run, p1);
    P[1] = thread(&Calculos::run, p2),
    P[0].join();
    double m = p1.media;
    P[2] = thread(Calculos::calcular_desvtipica(m), p3);
    P[1].join();
    P[2].join();

    cout << "#datos: " << N << endl;
    cout << "media: " << p1.media << endl;
    cout << "min: " << p2.min << endl;
    cout << "max: " << p2.max << endl;
    cout << "sigma: " << p3.desvtipica << endl;

    cout << "Fin\n";
    return 0;
}

我尝试更改此行:

P[2] = thread(Calculos::calcular_desvtipica(m), p3);

到:

P[2] = thread(p3.calcular_desvtipica(m), p3);

但它不起作用。问题在哪里?

0 个答案:

没有答案