I have no idea what's wrong with this vector implementation

This is a discussion on I have no idea what's wrong with this vector implementation within the C++ Programming forums, part of the General Programming Boards category; I have no idea what's wrong with following code. Code: //yada, yada, the usual include files using namespace std; typedef ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    I have no idea what's wrong with this vector implementation

    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.
    Last edited by cunnus88; 10-18-2006 at 08:10 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,856
    It is not possible to convert a vector<T2> into a double (for any type T2). That's what your static cast is trying to do.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Yes, that is why I later tried C style casts and just leaving it alone. None of them would assign a value to result[0]. I haven't declared it as a constant, so why it should do that is beyond me.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The first error message suggests that v2 is a vector<vector<int>>, not a vector<int>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    THANK YOU!

    I am an idiot.
    Last edited by Ken Fitlike; 10-18-2006 at 09:23 AM. Reason: Don't try to bypass the profanity filter, it's there for a reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An idea that's out of my reach of knowledge
    By Akkernight in forum Tech Board
    Replies: 12
    Last Post: 04-08-2009, 09:35 PM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 02:25 PM
  3. An Idea I have...
    By TechWins in forum Tech Board
    Replies: 1
    Last Post: 07-25-2003, 05:49 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Just an idea >?<
    By Cgawd in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-30-2002, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21