我用以下方式编写代码,当我使用c++ -std=c++0x -lpthread
程序编译它给出了编译错误。我多次检查过这个问题,但无法解决。
#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'
答案 0 :(得分:1)
你的text
是一个数组数组,而不是一个指针数组。因此,更改函数签名以使char参数char (*pdbn)[100]
和char (*text)[30]
或等效地成为char pdbn[][100]
和char text[][30]
。
(要明确的是:text
的每个元素都是char[30]
,而不是是char *
。)