3

以下代码是我作为一个更大项目的一部分编写的插值函数的一部分。此函数的第一个版本返回 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> ] 
4

2 回答 2

2

根据您的评论,在您的真实代码中,仅double传递了 foryarray而不是double*or std::vector<double>。这是一个足够小但不正确的复制的简单案例——真正的错误在于你的真实代码。

于 2011-05-20T15:17:54.243 回答
1

在您发布的代码中,您finterp使用myYVec = double*. 这可以用[0].

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

您作为第三个参数传入的任何类型显然都是不可索引的。

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

于 2011-05-20T15:20:07.703 回答