模板函数“下标需要数组类型”,但适用于较小的项目。为什么?
以下代码是我在一个较大项目中编写的插值函数的一部分。该函数的第一个版本返回 myScalar yval,但我将其修改为返回一个有关该函数是否有效的标志。
我的问题是这样的。以下代码在 codepad.org 和较小的 Visual Studio 项目中单独运行时都会进行编译。但在我的较大项目中,我收到错误 C2109“下标需要数组或指针类型”。可能出了什么问题?
提前致谢! -- 乔
using namespace std;
template <class myScalar, class myXVec, class myYVec>
int finterp(int mode, myXVec xarray, myYVec yarray, int num_pts, myScalar xval, myScalar &yval)
{
myScalar dx, dydx, xmin, xmax;
int success_flag = 0;
if (num_pts < 1) return success_flag;
yval = yarray[0]; //Visual Studio error C2109
//some more calculations are here
success_flag = 1;
return success_flag;
}
int main()
{
double *xvec, *yvec;
xvec = new double [5]; yvec = new double [5];
for (int i = 0; i < 5; i++)
{
xvec[i] = (double)i;
yvec[i] = (double)i+1;
}
double x, y;
x = 3.0;
int success = finterp(1, xvec, yvec, 5, x, y);
cout << y << " " << success << endl;
return 0;
}
输出:
1> j:\london_study\irasshell_2011-05-13\iras_src\templateutilityfunctions.h(74):
error C2109: subscript requires array or pointer type
1> j:\london_study\irasshell_2011-05-13\iras_src\hydpowclass.cpp(41) :
see reference to function template instantiation 'int finterp<double,std::vector<_Ty>,double>(int,myXVec,myYVec,int,myScalar,myScalar &)' being compiled
1> with
1> [
1> _Ty=double,
1> myXVec=std::vector<double>,
1> myYVec=double,
1> myScalar=double
1> ]
The following code is part of an interpolation function I wrote as part of a larger project. The first version of this function returned the myScalar yval, but I modified it to return a flag on whether or not the function worked.
My question is this. The following code compiles when run by itself both on codepad.org and in a smaller Visual Studio project. In my larger project, though, I am getting error C2109 "subscript requires array or pointer type." What could be going wrong?
Thanks in advance! -- Joe
using namespace std;
template <class myScalar, class myXVec, class myYVec>
int finterp(int mode, myXVec xarray, myYVec yarray, int num_pts, myScalar xval, myScalar &yval)
{
myScalar dx, dydx, xmin, xmax;
int success_flag = 0;
if (num_pts < 1) return success_flag;
yval = yarray[0]; //Visual Studio error C2109
//some more calculations are here
success_flag = 1;
return success_flag;
}
int main()
{
double *xvec, *yvec;
xvec = new double [5]; yvec = new double [5];
for (int i = 0; i < 5; i++)
{
xvec[i] = (double)i;
yvec[i] = (double)i+1;
}
double x, y;
x = 3.0;
int success = finterp(1, xvec, yvec, 5, x, y);
cout << y << " " << success << endl;
return 0;
}
Output:
1> j:\london_study\irasshell_2011-05-13\iras_src\templateutilityfunctions.h(74):
error C2109: subscript requires array or pointer type
1> j:\london_study\irasshell_2011-05-13\iras_src\hydpowclass.cpp(41) :
see reference to function template instantiation 'int finterp<double,std::vector<_Ty>,double>(int,myXVec,myYVec,int,myScalar,myScalar &)' being compiled
1> with
1> [
1> _Ty=double,
1> myXVec=std::vector<double>,
1> myYVec=double,
1> myScalar=double
1> ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的评论,在您的实际代码中,仅为
yarray
传入double
而不是double*
或std: :向量<双>
。这是一个简单的例子,有一个足够小但不正确的重现——真正的错误在于你的真实代码中。As per your comment, in your real code a mere
double
is being passed in foryarray
rather than adouble*
orstd::vector<double>
. This is a simple case of having a sufficiently small, but incorrect, repro -- the real error lies in your real code.在您发布的代码中,您使用
myYVec = double*
调用finterp
。可以使用[0]
对其进行索引。当您在较大的项目中使用它时,您如何调用
finterp
? Visual Studio 应该在 c2109 之后的错误消息中告诉您。无论您作为第三个参数传入的类型是什么,显然都是不可索引的。
编辑啊,您已经用错误消息更新了您的问题。当您使用
myYVec = double
调用finterp
时会发生错误 - 这是不可索引的。我认为您打算使用double*
。In the code you've posted, you're calling
finterp
withmyYVec = double*
. This can be indexed just fine with[0]
.When you use this in a larger project, how are you calling
finterp
? Visual Studio should tell you in the error messages after the c2109.Whatever type you're passing in as the third parameter is apparently non-indexable.
EDIT Ah, you've updated your question with the error message. The error occurs when you call
finterp
withmyYVec = double
-- which is NOT indexable. I think you meant to use adouble*
.