C++ g++;调用函数时出现编译器错误(vector<;vector<;int>;)
有人能告诉我为什么这个简单的函数调用返回底部显示的编译器错误吗C++ g++;调用函数时出现编译器错误(vector<;vector<;int>;),c++,pass-by-reference,stdvector,C++,Pass By Reference,Stdvector,有人能告诉我为什么这个简单的函数调用返回底部显示的编译器错误吗 //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> > // Th
//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;
}
为什么编译器将int2D&
与int
混淆?调用、函数原型和函数始终是int2D
类型
//这是我的编译器版本
Mac OS X 10.8.3上的i686-apple-darwin11-llvm-g++-4.2
顺便说一下,这与我在使用g++4.3的Linux机器上遇到的错误相同
感谢您的帮助,Mike您在这行有语法错误:
// This is the function call where error is indicated
int DumpL2toFile(L2Data); (**line 90 - error indicated here**)
如果调用DumpL2toFile
。您不再需要返回类型。这样,编译器将其视为函数声明,但是,L2Data
不是类型,它是int2D
的对象,这会触发编译错误
同时,compileerror在deprocessL2()
函数中显示error,而您并没有发布此部分的代码
// This is the function call where error is indicated
int DumpL2toFile(L2Data); (**line 90 - error indicated here**)
那不是函数调用!假设这一行出现在函数体内部(代码中不清楚),函数调用将是:
DumpL2toFile(L2Data); // No int
哦,你只需要知道这些。但是如果您感到好奇,编译器会像解析语句一样解析您的语句
int AnyOldIdentifier(L2Data);
它是名为
AnyOldIdentifier
的int
变量的声明,初始化为值L2Data
。它不能将int
初始化为L2Data
,因为L2Data
是int2D
,而不是int
,谢谢大家!!事实上是我自己想出来的。只是一个愚蠢的剪切粘贴问题。我将函数原型切碎,剪切并粘贴到类主体中,但没有完成编辑!!对不起,浪费了你的时间。愚蠢有时也是愚蠢的。我想当我和侄女一起寻找复活节彩蛋时撞到我的头并没有帮助:-)事实上我有一个不同的问题。为什么编译器没有抱怨这是一个影子声明?毕竟我已经在.h文件中声明了这个函数。编译器不应该像对待阴影变量那样抱怨阴影方法吗?
int AnyOldIdentifier(L2Data);