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!

Nice start.

The main cause of problems is one of types on lines 14 and 15 in main().

A template type is an incomplete type. It cannot be instantiated into an actual variable type without further information: the template arguments.

Hence, you cannot declare a variable as vector <T> v; , since T is not a valid type. You must decide what type you wish to use. Since you are reading ints from the user (as per line 19) you might as well have a vector of them. Get rid of line 14 and change line 15 to read vector <int> v; If you try compiling again you'll notice that your list of errors has shortened considerably. :)


Next, line 57 (swapValues()) does not declare a template function. So two things are wrong with it: 1) it does not satisfy the prototype on lines 9 and 10, and 2) the compiler will barf on the unknown type T. Fix it by adding the line template <class T> in front of the swapValues() definition.


There is one other thing you need to notice. Line 46 assumes that T is an alias for int when you declare your indexOfMax variable. That's bad. What if it were string? or bool? Change it to T max = v[startIndex]; int indexOfMax = startIndex; Hmm, I think that's it. I hope I got the line numbering right...
Have fun!

Awesome! Thanks a lot. I think I might be able to do this...

You must give a initialed num for a vect obj.
For example:
vector<int> nJingle(8);//right
vector<int> nJingle;/erro

You can initial every element to 0 as followed: vector<int> nJingle(8,0;//There are 8 element in this vector obj and every member's value is initialed to zero.

template<class T>
int indexoflargest(const vector<T> v, int startIndex, int vsize);

Wouldn't it be better/faster to pass the vector by reference ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.