...

View Full Version : Templated function help



GriffTech
02-20-2004, 02:44 AM
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;
}
}

}

Mitch
02-20-2004, 05:37 PM
no comment...lol....i odnt know what your talking abvout....ou might wanna say like what language your using.

shmoove
02-22-2004, 10:10 AM
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:


<T> temp;

instead of:


vector<T> temp;


shmoove

shmoove
02-22-2004, 12:46 PM
One more thing...
Since your main loop runs up until vecArray.size():


for(unsigned int i=0; i < vecArray.size(); i++)
then accessing the [i + 1] element:


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:


for(unsigned int i=0; i < vecArray.size() - 1; i++)
since you don't have anything to compare to the last element.

shmoove

Mhtml
02-22-2004, 02:42 PM
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++.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum