Multiplying matrices: error: expected primary-expr

2019-09-20 07:41发布

问题:

I am trying to write a program that is supposed to multiply matrices using threads.
I am supposed to fill the matrices using random numbers in a thread. I am compiling in g++ and using PTHREADS. I have also created a struct to pass the data from my command line input to the thread so it can generate the matrix of random numbers. The sizes of the two matrices are also passed in the command line as well.

I keep getting: main.cpp:7: error: expected primary-expression before 'struct' my code @ line 7 =:

struct a{  
     int Arow;    
     int Acol;   
     int low;   
     int high;
 };

My inpust are :
Sizes of two matrices ( 4 arguments) high and low ranges in which o generate the random numbers between.

Complete code:

[headers]  
using namespace std;

void *matrixACreate(struct *);

void *status;


int main(int argc, char * argv[])
{ 


  int Arow = atoi(argv[1]); // Matrix A  
  int Acol = atoi(argv[2]); // WxX  
  int  Brow = atoi(argv[3]); // Matrix B  
  int Bcol = atoi(argv[4]); // XxZ,   
  int low = atoi(argv[5]); // Range low  
  int high = atoi(argv[6]);

struct a{
     int Arow; // Matrix A    
     int Acol; // WxX  
     int low; // Range low  
     int high;
 };


 pthread_t matrixAthread;
 //pthread_t matrixBthread;
 pthread_t runner;
 int error, retValue;

 if (Acol != Brow)
 {
     cout << " This matrix cannot be multiplied. FAIL" << endl;
     return 0;
 }

 error = pthread_create(&matrixAthread, NULL, matrixACreate, struct *a);  
 //error = pthread_create(&matrixAthread, NULL, matrixBCreate, sendB);  
 retValue = pthread_join(matrixAthread, &status);  
 //retValue = pthread_join(matrixBthread, &status);  

 return 0;
}
void matrixACreate(struct * a)
{
    struct a *data = (struct a *) malloc(sizeof(struct a));  
    data->Arow = Arow;  
    data->Acol = Acol;  
    data->low = low;  
    data->high = high;  
    int range = ((high - low) + 1);  
    cout << Arow << endl<< Acol << endl;  
    }// just trying to print to see if I am in the thread

回答1:

You can't declare a structs in the middle of a function and expect your other functions to know about it, you need to move it before main. You also have all kinds of problems in the rest of your code, I would suggest you consult some tutorials/books on C and structures. I tried to fix up the code the best I could, but to be honest I wasn't entirely sure what you were trying to do...

struct a {
    int Arow;
    int Acol;
    int Brow;
    int Bcol;
    int low;
    int high;
};

void *matrixACreate(struct a*);

void *status;

int main(int argc, char * argv[]) {

    struct a matrix_mult_info;

    matrix_mult_info.Arow = atoi(argv[1]); // Matrix A
    matrix_mult_info.Acol = atoi(argv[2]); // WxX
    matrix_mult_info.Brow = atoi(argv[3]); // Matrix B
    matrix_mult_info.Bcol = atoi(argv[4]); // XxZ,
    matrix_mult_info.low = atoi(argv[5]); // Range low
    matrix_mult_info.high = atoi(argv[6]);


    pthread_t matrixAthread; //pthread_t matrixBthread; pthread_t runner;
    int error, retValue;

    if (matrix_mult_info.Acol != matrix_mult_info.Brow) { cout << " This matrix cannot be multiplied. FAIL" << endl; return 0; }

    /* Note that since you're creating a new thread, you can't access matrix_mult_info
       simultaneously in both threads without using a lock */
    error = pthread_create(&matrixAthread, NULL, matrixACreate, &matrix_mult_info);
    //error = pthread_create(&matrixAthread, NULL, matrixBCreate, sendB);
    retValue = pthread_join(matrixAthread, &status);
    //retValue = pthread_join(matrixBthread, &status);

    return 0;
}
void matrixACreate(struct a *matrix) {
    struct a *data = (struct a *) malloc(sizeof(struct a));
    data->Arow = matrix->Arow;
    data->Acol = matrix->Acol;
    int range = ((matrix->high - matrix->low) + 1);
    cout << Arow << endl<< Acol << endl;
    free(data);
}// just trying to print to see if I am in the thread


回答2:

void *matrixACreate(struct *);

That's a problem. struct by itself is not a typename, so you can't have a pointer to it.

You'll need to say

void *matrixACreate(struct a *);

And then that looks for a struct a at global scope, so you'll need to define struct a at global scope, instead of inside main.


Ironically, @user470379 had the right solution even if for the wrong reasons. But everyone browbeat him into deleting his answer.



标签: c++ g++ pthreads