2011-03-07 69 views
-1

我想寫一個程序,應該使用線程乘以矩陣。
我應該用一個線程中的隨機數填充矩陣。 我正在編譯g ++並使用PTHREADS。我還創建了一個結構,將數據從我的命令行輸入傳遞給線程,以便它可以生成隨機數字矩陣。兩個矩陣的大小也在命令行中傳遞。乘法矩陣:錯誤:預期初始表達式'結構'之前

我一直得到:main.cpp中:7:錯誤:預期 '結構' 我的代碼@行之前基本表達式7 =:

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

我inpust是:
兩個矩陣的大小( 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 
+6

向我們展示您的結構之前的代碼*。 – Erik 2011-03-07 22:31:55

+1

當編譯器說錯誤在第7行時;這意味着直到第7行才能理解代碼。有時,很多時候,錯誤是在1或2行之前......但代碼仍然有意義。仔細觀察第4,5行,** 6 **和7. – pmg 2011-03-07 22:35:21

+0

這可以通過在不屬於它的位置放一個分號來實現,但需要看到更完整的代碼纔能有任何想法。 – AJG85 2011-03-07 22:37:29

回答

3

你不能在函數中聲明struct S和期待你的其他功能,瞭解它,你需要之前主要將其移動。你在其他代碼中也有各種各樣的問題,我建議你參考一些關於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

我的編譯器接受「本地類型定義」。 C標準也允許他們(不瞭解C++)。 – pmg 2011-03-07 23:24:55

+1

根本不是真的。你甚至嘗試過嗎? – 2011-03-07 23:25:34

+0

@pmg:它們在C++中是絕對允許的,但是,本地類型定義不能用作模板參數(直到C++ 0x修復了這個)。 – 2011-03-07 23:27:22

0
void *matrixACreate(struct *); 

這是一個問題。 struct本身不是一個typename,所以你不能有一個指向它的指針。

你需要說

void *matrixACreate(struct a *); 

然後是尋找一個struct a在全球範圍內,所以你需要在全局範圍內定義struct a,而不是內部的主。


具有諷刺意味的是,@ user470379即使出於錯誤的原因也有正確的解決方案。但是每個人都會威脅他刪除他的答案。