不能取值对象--C ++

时间:2015-10-11 16:23:42

标签: c++ c++11 syntax-error

我在考虑" p1.media"的价值方面遇到了问题。价值是媒体:6.91026e-310,必须像1000,5000,....我尝试了太多的解决方案,但任何人的工作。这是代码:

  

Calculos.h代码

package exercise4;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;

public class BarajaTest 

{
    @Test
    public void testGetCard() {
        int position = 0;
        Deck instance = new Deck();
        Card expResult = new (CardValue.ACE,Suit.HEARTS);
        Card result = instance.getCard(position);
        assertEquals(expResult, result);}
}
  

Calculos.cpp代码

#ifndef CALCULOS_H
#define CALCULOS_H
//includes
#define N 100
using namespace std;

class Calculos {
public:
    Calculos(double T[], int op);  
    Calculos(double T[], int op, double media);
    Calculos(); //constructor por defecto
    void run();

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

Ejercicio.cpp代码

//includes
#include "Calculos.h"
using namespace std;

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

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

};
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 {  //op=3 calcular desviacion tipica
        desvtipica = 3.0; //partially
    }
};

2 个答案:

答案 0 :(得分:5)

将参数传递给// Play the stream NSString *wifiStreamAddress = @"http://yourmoviefile.m3u8"; AVPlayer *player = [[AVPlayer alloc] initWithURL: [NSURL URLWithString: wifiStreamAddress] ]; AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init]; playerViewController.player = player; // Keep pointers to player and controller using retained properties: self.player = player; self.playerViewController = playerViewController; [player release]; [playerViewController release]; [self presentViewController: playerViewController animated: true completion: ^{ [self.player play]; }]; 构造函数的方式存在问题。

变化:

thread

到:

   P[0] = thread(&Calculos::run, p1); // creates a copy of p1

现在输出是:

   P[0] = thread(&Calculos::run, std::ref(p1)); // pass p1 by reference
  

备注

     

线程函数的参数按值移动或复制。   如果需要将引用参数传递给线程函数,   它必须被包装(例如#datos: 100 media: 547.278 Fin std::ref)。

     

来源:http://en.cppreference.com/w/cpp/thread/thread/thread   (强调我的)

在您的代码中,std::crefp1.media,因为您没有对其执行任何操作。 您使用了0的临时副本。

答案 1 :(得分:3)

问题是你使用std::thread的构造函数。请参阅它的第三个定义here。第P[0] = thread(&Calculos::run, p1);行正在创建p1副本,然后对其进行操作。请尝试使用简单的参考捕获:

P[0] = thread([&p1](){ p1.run(); });

或者,我相信您也可以尝试传递p1这样的地址:

P[0] = thread(&Calculos::run, &p1);

我知道该语法适用于std::bind之类的内容,但我不确定std::thread