0

谁能告诉我为什么这个简单的函数调用会返回底部显示的编译器错误?

//This is a type definition that I use below to simplify variable declaration
typedef vector<int> islice;
typedef vector<islice> int2D;
// therefore int2D is of type  vector<vector<int> >

// This is the function prototype in the DFMS_process_spectra_Class.hh file
int DumpL2toFile(int2D&);

// This is the type declaration in the caller
int2D L2Data;

// This is the function call where error is indicated
int DumpL2toFile(L2Data);      (**line 90 - error indicated here**)

// this is the function body 
int DFMS_process_spectra_Class::DumpL2toFile(int2D& L2) {

    string file=sL3Path+L2Info.fileName;
    fstream os;
    os.open(file.c_str(), fstream::out);
    os << "Pixel   A-counts   B-counts" << endl;
    char tmp[80];
    for (int i=0; i<512; ++i) {
        sprintf(tmp,"%5d    %8d    %8d\n",L2[i][0],L2[i][1],L2[i][2]);
        os << string(tmp) << endl;
    }

    os.close();

    return 1;
}

//这是编译命令和错误

g++ -w -g -c src/DFMS_process_spectra_Class.cc -o obj/DFMS_process_spectra_Class.o
src/DFMS_process_spectra_Class.cc: 
In member function 'int   DFMS_process_spectra_Class::processL2()':
 src/DFMS_process_spectra_Class.cc:90: error: 
                      cannot convert 'int2D' to 'int' in initialization

为什么编译器会int2D&混淆int?调用、函数原型和函数的int2D类型一致!!

//这是我在 Mac OS X 10.8.3 上的编译器版本 i686-apple-darwin11-llvm-g++-4.2

顺便说一句,这与我在使用 g++ 4.3 的 Linux 机器上遇到的错误相同

感谢您的帮助,迈克

4

2 回答 2

1
// This is the function call where error is indicated
int DumpL2toFile(L2Data);      (**line 90 - error indicated here**)

That's not a function call! Assuming that this line occurs inside a function body (which is not clear from your code), the function call would be:

DumpL2toFile(L2Data); // No int

OP, that's all you need to know. But if you are curious, the compiler is parsing your statement as if it were

int AnyOldIdentifier(L2Data);

which is a declaration of an int variable called AnyOldIdentifier, initialised to the value L2Data. And it can't initialise an int to L2Data, because L2Data is an int2D, not an int.

于 2013-03-31T22:02:27.323 回答
0

您在这一行有语法错误:

   // This is the function call where error is indicated
  int DumpL2toFile(L2Data);      (**line 90 - error indicated here**)

如果你打电话DumpL2toFile。您不再需要返回类型。这样,编译器将其视为函数声明,但是,L2Data它不是类型,而是 的对象int2D,这会触发编译错误

同时,编译错误说错误 insdeprocessL2()函数,而您没有发布这部分的代码。

于 2013-03-31T20:10:15.860 回答