Web Analytics Made Easy -
StatCounter Templated function help - CodingForum

Announcement

Collapse
No announcement yet.

Templated function help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Templated function help

    Posted: Fri Feb 20, 2004 1:28 am Post subject: Templated function for vectors

    --------------------------------------------------------------------------------

    I am trying to write a templated function for vectors of different classes I have written. The templated function is an insertion sort, but I am gettign some binary errors. I have also overloaded the <, ==, << and >> operators within the Course class

    Here is a bit of my code

    template <typename T>
    void insertSort(vector<T> vecArray);

    within main I have declared a vector of type Course.

    vector<Course> courseVec;
    I have opened a file and filled the vector
    I call the function

    insertSort(courseVec);

    here is my insertion code for the templated function

    here is the errors I am gettting. Can anyone help

    error C2678: binary '<' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
    with
    [
    _Ty=Course
    ]
    error C2678: binary '<' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
    with
    [
    _Ty=Course
    ]
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::allocator<_Ty>::value_type' (or there is no acceptable conversion)
    with
    [
    _Ty=Course
    ]
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
    with
    [
    _Ty=Course
    ]








    template <typename T>
    void insertSort(vector<T> vecArray)
    {
    vector<T> temp;


    for(unsigned int i=0; i < vecArray.size(); i++)
    {
    temp = vecArray[i+1];
    if(temp < vecArray[i])
    {
    for(int j=i; temp < vecArray[j] && j >=0; j--)
    {
    vecArray[j+1] = vecArray[j];
    }

    vecArray[j+1] = temp;
    }
    }

    }

  • #2
    no comment...lol....i odnt know what your talking abvout....ou might wanna say like what language your using.
    Umm...dude?
    Hell just broke loose...

    Comment


    • #3
      in the insertSort() method, you're creating a Vector called "temp", but you try to assign and compare it to Vector elements.
      Maybe if you used:
      Code:
      <T> temp;
      instead of:
      Code:
      vector<T> temp;
      shmoove

      Comment


      • #4
        One more thing...
        Since your main loop runs up until vecArray.size():
        Code:
        for(unsigned int i=0; i < vecArray.size(); i++)
        then accessing the [i + 1] element:
        Code:
        temp = vecArray[i+1];
        will cause you to access an element outside the vector's range. So I imagine you want that outer loop to look like:
        Code:
        for(unsigned int i=0; i < vecArray.size() - 1; i++)
        since you don't have anything to compare to the last element.

        shmoove

        Comment


        • #5
          Originally posted by Mitch
          no comment...lol....i odnt know what your talking abvout....ou might wanna say like what language your using.
          It's obviously C++.
          Omnis mico antequam dominus Spookster!

          Comment

          Working...
          X