2013-07-30 31 views
-4

我寫的代碼以下面的方式,當我與c++ -std=c++0x -lpthread程序將其賦予編譯錯誤編譯。我多次檢查了這個問題,但無法解決。語法錯誤,同時傳遞參數,通過功能的std ::線程

#include <iostream> 
#include <string.h> 
#include <math.h> 
#include <stdlib.h> 
#include <cstdlib> 
#include <string> 
#include <stdio.h> 
#include <thread> 
using namespace std; 
#define ATOM 200000 
#define WATER 10000 
#define POLAR 20000 
int main() { 

     char text[ATOM][30]; 
     int now=0; 
     int noh=0, na=0; 
     int nop=0, lines=0; 
     char inputfile[30]; 
     char p[30]; 
     string pdbdata; 
    cout<<"enter input file"<<endl; 
     cin>>inputfile; 
    char pdbn[10000][100]; 
     char pdb[100]; 
    float x, y ,z; 
     FILE *inputf=fopen(inputfile,"r"); 
     char c; 
     int i; 
    int c1 = 0; 
    if(inputf == NULL) { 
       cout<<"cannot open inputfile"; 
       return 0; 
     } 
    while (1) { 
       if(fscanf(inputf, "%s", pdb) == EOF) { 
         fclose(inputf); 
         break; 
       } else { 
         strcpy(pdbn[lines],pdb); 
      lines++; 
       } 
       if(lines == 0) 
         return 0; 
       na = 0; 
       now = 0; 
       nop = 0; 
       noh = 0; 
       FILE *pdbf = fopen (pdb, "r"); 
       float a, b; 
       if (pdbf == NULL) 
         return 1; 
       while (1) { 
         if ((c=fgetc(pdbf)) != EOF) { 
           p[0] = c; 
           for(i=1; i<30; i++) 
             p[i] = fgetc(pdbf); 
           p[30] = '\0'; 
           pdbdata = p; 
           c1++; 
           fscanf (pdbf, "%f%f%f%f%f\n", &x, &y, &z, &a, &b); 
           size_t f = pdbdata.find ("ATOM"); 
           if(f!=std::string::npos) { 
             strcpy(text[na],p); 
             na++; 
             f=pdbdata.find("WAT"); 
             if((f!=std::string::npos)&&(p[13]=='O')) { 
               now++; 
             } else if((p[13]=='O') || (p[13]=='N')) { 
               nop++; 
             } 
             if((p[12]=='H') || (p[13] == 'H')) 
               noh=noh+1; 
           } 
         } else { 
           fclose(pdbf); 
           break; 
         } 
       } 

     } 
    float X[na * lines], Y[na * lines], Z[na * lines]; 
    int pol[lines * nop]; 
    int wat[lines * now]; 
    int fsw[lines * now]; 
    int na1, now1, nop1; 
     inputf = fopen(inputfile,"r"); 
    int m = 1; 
    int SnumCPU = sysconf(_SC_NPROCESSORS_ONLN); 
    thread tr1[SnumCPU]; 
    int nfiles = lines/(SnumCPU - 1); 
     for (i = 0; i < (SnumCPU - 1); i++) { 
       int in = i * nfiles; 
       tr1[i] = thread(readfile1, in, nfiles, &pdbn[0], &X[0], &Y[0], &Z[0], &wat[0], &pol[0], now, nop, na, &text[0]); 
     } 
     if (lines % (SnumCPU - 1) != 0) 
       tr1[(SnumCPU - 1)] = thread(readfile1, (SnumCPU - 1) * nfiles, (lines % (SnumCPU - 1)), &pdbn[0], &X[0], &Y[0], &Z[0], &wat[0], &pol[0], now, nop, na, &text[0]); 
     for (i = 0; i < (SnumCPU - 1); i++) { 
       tr1[i].join(); 
     } 
     if (lines % (SnumCPU - 1) != 0) 
       tr1[SnumCPU - 1].join(); 
    return 0; 
} 

和定義爲readfile1功能如下

void readfile1 (int istart, int iend, char *pdbn[100], float *X, float *Y, float *Z, int *wat, int *pol, int now, int nop, int na, char *text[30]) { 
    float x, y, z, a, b; 
    for(int i = istart; i< istart + iend; i++) { 
       string pdbdata; 
       char p[40]; 
     int na1 = 0; 
       int now1 = 0; 
       int nop1 = 0; 
       FILE *pdbf=fopen(pdbn[i],"r"); 
       int pdbid = na * i; 
     int watid = i * now; 
     int polid = i * nop; 
     float a, b; 
       if(pdbf == NULL) 
         break; 
     char c; 
       while (1) { 
         if((c = fgetc(pdbf))!=EOF) { 
           p[0] = c; 
           for(i = 1; i < 30; i++) 
           p[i] = fgetc (pdbf); 
           p[29] = '\0'; 
           pdbdata = p; 
           fscanf(pdbf, "%f%f%f%f%f\n", &x, &y, &z, &a, &b); 
           size_t f; 
           strcpy (text[na], p); 
           X[pdbid + na1] = x; 
           Y[pdbid + na1] = y; 
           Z[pdbid + na1] = z; 
           f = pdbdata.find("WAT"); 
           if ((f != std::string::npos) && (p[13] == 'O')) { 
            wat[watid + now1] = na1; 
             now1++; 
           } else if((p[13] == 'O') || (p[13] == 'N')) { 
            pol[polid + nop1] = na1; 
             nop1++; 
           } 
       na1++; 
         } else { 
          fclose (pdbf); 
           break; 
      } 
       } 

    } 
} 

給錯誤如下

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/functional:70, 
       from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:39, 
       from parallel3.cpp:8: 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional: In member function ‘typename std::result_of<_Functor(typename std::result_of<std::_Mu<_Bound_args, std::is_bind_expression::value, (std::is_placeholder::value > 0)>(_Bound_args, std::tuple<_UElements ...>)>::type ...)>::type std::_Bind<_Functor(_Bound_args ...)>::__call(const std::tuple<_UElements ...>&, std::_Index_tuple<_Indexes ...>) [with _Args = , int ..._Indexes = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, _Functor = void (*)(int, int, char**, float*, float*, float*, int*, int*, int, int, int, char**), _Bound_args = int, int, char (*)[100], float*, float*, float*, int*, int*, int, int, int, char (*)[30]]’: 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1191: instantiated from ‘typename std::result_of<_Functor(typename std::result_of<std::_Mu<_Bound_args, std::is_bind_expression::value, (std::is_placeholder::value > 0)>(_Bound_args, std::tuple<_UElements ...>)>::type ...)>::type std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args& ...) [with _Args = , _Functor = void (*)(int, int, char**, float*, float*, float*, int*, int*, int, int, int, char**), _Bound_args = int, int, char (*)[100], float*, float*, float*, int*, int*, int, int, int, char (*)[30]]’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:114: instantiated from ‘void std::thread::_Impl<_Callable>::_M_run() [with _Callable = std::_Bind<void (*(int, int, char (*)[100], float*, float*, float*, int*, int*, int, int, int, char (*)[30]))(int, int, char**, float*, float*, float*, int*, int*, int, int, int, char**)>]’ 
parallel3.cpp:150: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: error: cannot convert ‘char (*)[100]’ to ‘char**’ in argument passing 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: error: return-statement with a value, in function returning 'void' 
+7

你應該認真考慮一下減少這種下降到能重現問題一個簡短的自我包含的例子。我相信這裏顯示的大部分代碼都是不相關的。 – juanchopanza

+2

我們甚至不知道你的編譯器產生了什麼錯誤... – Nbr44

+0

在目前的形式中,問題在這裏沒有地方。投票擱置。 – Dariusz

回答

1

text是數組的數組,而不是指針數組。因此,更改函數簽名以使字符參數char (*pdbn)[100]char (*text)[30]或等效地爲char pdbn[][100]char text[][30]

(要明確表示:text每個元素都是一個char[30],而不是一個char *。)