乘法矩阵:错误:'struct'之前的预期primary-expression

时间:2011-03-07 22:28:47

标签: c++ g++ pthreads

我正在尝试编写一个应该使用线程来增加矩阵的程序 我应该在一个线程中使用随机数填充矩阵。 我正在用g ++编译并使用PTHREADS。我还创建了一个结构来将数据从命令行输入传递给线程,这样它就可以生成随机数矩阵。两个矩阵的大小也在命令行中传递。

我一直得到:main.cpp:7:错误:在'struct'之前预期的primary-expression 我的代码@第7行=:

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

我的意思是:
两个矩阵的大小(4个参数) 高低范围,其中o之间产生随机数。

完整代码:

[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

2 个答案:

答案 0 :(得分:3)

你不能在函数中间声明struct并期望你的其他函数知道它,你需要在main之前移动它。你在其余的代码中也遇到了各种各样的问题,我建议你查阅关于C和结构的一些教程/书籍。我尽力修复代码,但说实话,我并不完全确定你要做的是什么......

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

答案 1 :(得分:0)

void *matrixACreate(struct *);

这是一个问题。 struct本身不是类型名称,因此您无法指向它。

你需要说

void *matrixACreate(struct a *);

然后在全局范围内查找struct a,因此您需要在全局范围内定义struct a,而不是在main内部。


具有讽刺意味的是,即使出于错误的原因,@ user470379也有正确的解决方案。但是每个人都谴责他删除他的答案。