Reputation: 260
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> ]
Upvotes: 3
Views: 797
Reputation: 9172
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*
.
Upvotes: 1
Reputation: 62975
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.
Upvotes: 2