Hi everyone,
This is my first post on the forum, although I've been coming here to find search for help for some time now. I'm taking a C++ programming class, and am having trouble with my most recent assignment. I'm not here for straight-up answers, just a little help.
I was supposed to write a program that takes in some elements to put into a vector, and then sorts them from greatest to least. After some time, I came up with this code, which I think works fine:
include<iostream>
using std::cout;
using std::endl;
#include <vector>
using namespace std;
int indexoflargest(const vector<int> v, int startIndex, int vsize);
void swapValues(int& variable1, int& variable2);
//void sort(vector<int> v);
int main( )
{
vector<int> v;
cout << "Enter a list of elements.\n"
<< "Press Enter and then hit CTRL-D.\n";
int val;
while (cin >> val)
{
v.push_back(val);
}
cout << "You entered:\n";
for (unsigned int i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
int placeholder;
for (unsigned int index = 0; index < v.size()-1; index++)
{
placeholder = indexoflargest(v, index, v.size());
swapValues(v[index], v[placeholder]);
}
for (unsigned int i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
int indexoflargest(const vector<int> v, int startIndex, int vsize)
{
int max = v[startIndex], indexOfMax = startIndex;
for (int index = startIndex + 1; index < vsize; index++)
if (v[index] > max)
{
max = v[index];
indexOfMax = index;
}
return indexOfMax;
}
void swapValues(int& variable1, int& variable2)
{
int temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
My problems arose when I tried to do the next part of the assignment, which was to make this code work for all elements, not just integers, using a template class. My failed attempt looks like this:
include<iostream>
using std::cout;
using std::endl;
#include <vector>
using namespace std;
template<class T>
int indexoflargest(const vector<T> v, int startIndex, int vsize);
template<class T>
void swapValues(T& variable1, T& variable2);
int main( )
{
template<class T>;
vector<T> v;
cout << "Enter a list of elements.\n"
<< "Press Enter and then hit CTRL-D.\n";
int val;
while (cin >> val)
{
v.push_back(val);
}
cout << "You entered:\n";
for (unsigned int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
int placeholder;
for (unsigned int index = 0; index < v.size()-1; index++)
{
placeholder = indexoflargest(v, index, v.size());
swapValues(v[index], v[placeholder]);
}
cout << "From greatest to least, your vector is: ";
for (unsigned int i = 0; i < v.size( ); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
template<class T>
int indexoflargest(const vector<T> v, int startIndex, int vsize)
{ T max = v[startIndex], indexOfMax = startIndex;
for (int index = startIndex + 1; index < vsize; index++)
if (v[index] > max)
{
max = v[index];
indexOfMax = index;
}
return indexOfMax;
}
void swapValues(T& variable1, T& variable2)
{
T temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
I'm getting a lot of compilation errors that I can't correct, all having to do with the template class.
Is there something I'm missing about templates that would fix most of the problems, or are they just the sum of a lot of little mistakes?
Thanks in advance!