乘法矩阵:error:'struct'之前的预期主表达式 [英] Multiplying matrices: error: expected primary-expression before 'struct'

查看:396
本文介绍了乘法矩阵:error:'struct'之前的预期主表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,应该使用线程乘以矩阵。

我应该使用线程中的随机数填充矩阵。
我在g ++编译和使用PTHREADS。我还创建了一个结构来传递数据从我的命令行输入到线程,所以它可以生成随机数的矩阵。

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.

我一直在得到:main.cpp:7:error:expectedprimary-expression before'struct '
我的代码@第7行=:

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

我的inpust是:

两个矩阵的大小(4个参数)$ b

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

完成代码:

[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


推荐答案

你不能在函数的中间声明一个 struct ,并希望你的其他函数知道它,你需要移动它前主。你在其余的代码中也有各种各样的问题,我建议你参考一些关于C和结构的教程/书。我试图尽可能最好地修复代码,但老实说,我不完全确定你在做什么...

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

这篇关于乘法矩阵:error:'struct'之前的预期主表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆