kingcong3
kingcong3

Reputation: 193

Error with threads

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);
        }

Upvotes: 0

Views: 85

Answers (1)

cnicutar
cnicutar

Reputation: 182609

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.

Upvotes: 3

Related Questions