线程错误

发布于 2024-12-08 22:04:24 字数 1364 浏览 0 评论 0原文

嘿伙计们,我在这段代码上遇到了一些麻烦。我不认为这有什么问题。但它给了我错误,例如

hw2.cpp:35: error: request for member 'max2' in 'my_data', which is of non-class type '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:错误:请求“my_data”中的成员“max2”,该成员属于非类类型“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);
        }

Hey guys I am having a little trouble with this bit of code. I dont see anything wrong with it. but it is giving me errors such as

hw2.cpp:35: error: request for member ‘max2’ in ‘my_data’, which is of non-class type ‘thread_data*’
hw2.cpp:35: error: request for member ‘max’ in ‘my_data’, which is of non-class type ‘thread_data*’
hw2.cpp:36: error: request for member ‘max’ in ‘my_data’, which is of non-class type ‘thread_data*’
hw2.cpp:39: error: request for member ‘max2’ in ‘my_data’, which is of non-class type ‘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);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-12-15 22:04:24

嗯,my_data 是一个指向结构的指针,而不是一个结构。您必须使用取消引用(*)它才能获取结构。尝试:

my_data->max2 = my_data->max

基本上 my_data->max2(*my_data).max2 的语法糖。

Well, my_data is a pointer to a structure, not a structure. You have to use dereference (*) it to get to the structure. Try:

my_data->max2 = my_data->max

Basically my_data->max2 is syntactic sugar for (*my_data).max2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文