模板函数“下标需要数组类型”,但适用于较小的项目。为什么?

发布于 2024-11-08 20:53:10 字数 1573 浏览 0 评论 0原文

以下代码是我在一个较大项目中编写的插值函数的一部分。该函数的第一个版本返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-11-15 20:53:10

根据您的评论,在您的实际代码中,仅为 yarray 传入 double 而不是 double*std: :向量<双>。这是一个简单的例子,有一个足够小但不正确的重现——真正的错误在于你的真实代码中。

As per your comment, in your real code a mere double is being passed in for yarray rather than a double* or std::vector<double>. This is a simple case of having a sufficiently small, but incorrect, repro -- the real error lies in your real code.

赠佳期 2024-11-15 20:53:10

在您发布的代码中,您使用 myYVec = double* 调用 finterp。可以使用 [0] 对其进行索引。

当您在较大的项目中使用它时,您如何调用finterp? Visual Studio 应该在 c2109 之后的错误消息中告诉您。

无论您作为第三个参数传入的类型是什么,显然都是不可索引的。

编辑啊,您已经用错误消息更新了您的问题。当您使用 myYVec = double 调用 finterp 时会发生错误 - 这是不可索引的。我认为您打算使用double*

In the code you've posted, you're calling finterp with myYVec = 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 with myYVec = double -- which is NOT indexable. I think you meant to use a double*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文