Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 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
    New Coder
    Join Date
    Feb 2004
    Location
    Virginia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •