Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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++_Arrays_Multithreading_Pointers_Dynamic Arrays - Fatal编程技术网

C++ 线程和指针数组?

C++ 线程和指针数组?,c++,arrays,multithreading,pointers,dynamic-arrays,C++,Arrays,Multithreading,Pointers,Dynamic Arrays,我对下面的代码有一个问题,在线程完成工作后,数组的输出是一个rabish数据,而它在没有线程的情况下正常工作,任何人都可以帮助我解释原因 #include<iostream> #include <thread> using namespace std; class Case1{ public: int *pplAges[5]; Case1(){ *pplAges= new int[5]; } void addToAges(int Age, int i

我对下面的代码有一个问题,在线程完成工作后,数组的输出是一个rabish数据,而它在没有线程的情况下正常工作,任何人都可以帮助我解释原因

#include<iostream>
#include <thread> 

using namespace std;


class Case1{

public:
 int *pplAges[5];
 Case1(){
 *pplAges= new int[5];
 }

 void addToAges(int Age, int index){
 pplAges[index] = new int;
 *pplAges[index] = Age;
 cout<< *pplAges[index] <<endl;

 }


};

void f1(Case1 passedObj,int age,int index)
{

passedObj.addToAges(age,index);

}

void main(){

 Case1 C;

thread t1(f1,C,13,0);
t1.join();

thread t2(f1,C,14,2);
t2.join();

cout<<*C.pplAges[0]<<endl;
cout<<*C.pplAges[2]<<endl;
}
#包括
#包括
使用名称空间std;
类别个案1{
公众:
int*pplAges[5];
案例1(){
*pplAges=新整数[5];
}
void addToAges(整数年龄,整数索引){
pplAges[index]=新整数;
*pplAges[指数]=年龄;

cout线程复制它们的参数,因此您的线程永远不会更改
C
。如果您需要显式引用,则必须使用std::ref(或std::cref用于常量引用)对其进行包装:

void f1(案例1&passedObj、整数年龄、整数索引)
{
passedObj.addToAges(年龄、指数);
}
void main(){
案例1 C;
螺纹t1(f1,标准::参考(C),13,0);
t1.join();
螺纹t2(f1,标准::参考(C),14,2);
t2.连接();
不能尝试改变

void f1(Case1 passedObj,int age,int index)


并传递线程
&C

-1,因为缺少缩进。
void f1(Case1 passedObj,int age,int index)
void f1(Case1* passedObj,int age,int index)