大家好,我在处理这段代码时遇到了一些麻烦。我看不出有什么问题。但它给我错误,例如
hw2.cpp:35: 错误:请求'my_data'中的成员'max2',它是非类类型'thread_data*' hw2.cpp:35: 错误:请求'my_data'中的成员'max',它是非类类型'thread_data*' hw2.cpp:36: 错误:请求'my_data'中的成员'max',它是非类类型'thread_data*' hw2.cpp:39: 错误:请求“my_data”中的成员“max2”,它是非类类型“thread_data*” hw2.cpp:40: error: request for member ‘max2’ in ‘my_data’, which is of non-class type ‘thread_data*’
struct thread_data
{
char *file_name;
int max;
int max2;
};
struct thread_data thread_data_array[NUM_THREAD];
void *FindNum(void *threadArg)
{
int in_num;
struct thread_data *my_data;
my_data = (struct thread_data *) threadArg;
file.open (my_data.file_name);
if (file.is_open())
cout << "file can not be file"<<endl;
while (!file.eof())
{
file >> in_num;
if (in_num > my_data.max){
my_data.max2 = my_data.max;
my_data.max = in_num;
}
else if (in_num > my_data.max2){
my_data.max2 = in_num;
}
}
pthread_exit(NULL);
}
最佳答案
嗯,my_data
是一个指向结构的指针,而不是结构。您必须使用取消引用 (*
) 它才能到达结构。尝试:
my_data->max2 = my_data->max
基本上 my_data->max2
是 (*my_data).max2
的语法糖。
关于c++ - 线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695359/