I have no idea what's wrong with following code.
Code:
//yada, yada, the usual include files
using namespace std;
typedef unsigned int UINT;
const UINT BUFMAX = 1024;
const UINT CHARMAX = 32;
const UINT ARRMAX = 32;
const double EHTA = 0.3;
const double THRESHOLD = 1.0;
template <class T1, class T2>
double GetPrediction(vector<T1>& v1, vector<T2>& v2)
{
//v1 is passed as type vector<double>, v2 as type vector<int>
vector<T1> result(v1);
/*error occurs in the next line. I tried using C type casts, e.g. (double),
and I tried leaving it alone so there would be no type conversion: result[0] = v2[0].
Nothing works. They all throw an error such as:
c:\perceptron\perceptron.cpp(28): error C2440: '=' : cannot convert from
'std::allocator<_Ty>::value_type' to 'std::allocator<_Ty>::value_type'
with
[
_Ty=std::vector<int>
]
and
[
_Ty=double
] */
result[0] = static_cast<double>(v2[0]);
/* If I comment out the above line. It gets even weirder.
The error occurs in <algorithm> as follows:
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\algorithm(401): error C2664:
'double std::multiplies<_Ty>::operator ()(const _Ty &,const _Ty &) const' :
cannot convert parameter 1 from 'std::allocator<_Ty>::value_type' to 'const double &'
with
[
_Ty=double
]
and
[
_Ty=std::vector<int>
]
Reason: cannot convert from 'std::allocator<_Ty>::value_type' to 'const double'
with
[
_Ty=std::vector<int>
] */
transform(v2.begin() + 1, v2.end(), result.begin() + 1, result.begin() + 1, multiplies<double>());
return accumulate(result.begin() + 1, result.end(), 0) > THRESHOLD ? 1.0 : -1.0;
}
template <class T1, class T2>
void GetWeightVector(vector<T1>& vw, vector<vector<T2> >& vi)
{
//vw is passed as a vector<double> from main(). vi is vector<int>.
double t;
for(UINT i = 0; i < vi.size(); i++)
{
t = 0.3 * (vi[i][0] - GetPrediction(vw, vi));
vector<T1> result(vw);
transform(result.begin(), result.end(), result.begin(), bind2nd(multiplies<double>(), t));
transform(result.begin(), result.end(), vw.begin(), vw.begin(), minus<double>());
}
}
// main() comes after
Argghhhhhh! I've been staring it this thing for hours and I still don't know how to fix it.