samedi 11 juin 2016

Template Method 'For Loop' to Reverse Vector


I have a method that reverses the objects of a vector. I want to be able to use the method regardless of data type, so I'm using a template. EX : " 1 2 3 4 " becomes " 4 3 2 1" & "how now brown cow" becomes "cow brown now how"

This is my current template method.(Full code added for copy and paste purposes)

#include "stdafx.h"  
#include <iostream>  
using namespace std;
template <class T>
void fillVector(vector<T>& vect, const T array[], int size)
{
for (int index = 0; index < size; ++index)
    vect.push_back(array[index]);
}

template<class T>
void reverse(vector<T>& vect)
{

    if (vect.empty())
    {
        throw EmptyVectorException();
    }

    else
    {

        int endVal = vect.size();

        for (int x = 0, y = endVal-1; x < (endVal / 2); x++, y--)
        {
            T temp = vect[x];
            vect[x] = vect[y];
            vect[y] = temp;
        }
    }

}


template<class T>
void output(const vector<T>& vect)
{
    for (int index = 0; index < static_cast<int>(vect.size()); index++)
    {
        cout << vect[index] << " ";
    }
}

int main()
{

const string  strings[] = { "How ", "Now ", "Brown ", "Cow" };
const char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
const int intNmbrs[] = { 1, 3, 5, 7, 9, 11, 13 };
const double dblNmbrs[] = { 11.1, 22.2, 33.3, 44.4, 55.5, 66.6 };

const int STRINGS_SIZE = sizeof(strings) / sizeof(strings[0]);
const int CHARS_SIZE = sizeof(chars) / sizeof(chars[0]);
const int INTS_SIZE = sizeof(intNmbrs) / sizeof(intNmbrs[0]);
const int DOUBLES_SIZE = sizeof(dblNmbrs) / sizeof(dblNmbrs[0]);

vector<int> intVector;
vector<double> doubleVector;
vector<string> stringVector;
vector<char> charVector;

// Fill the "vector" objects
fillVector(intVector, intNmbrs, INTS_SIZE);
fillVector(doubleVector, dblNmbrs, DOUBLES_SIZE);
fillVector(stringVector, strings, STRINGS_SIZE);
fillVector(charVector, chars, CHARS_SIZE);

vector<int> emptyVector;

cout << "nExample #3: Reverse the sequence of all "vector" objects";
reverse(intVector);
reverse(doubleVector);
reverse(stringVector);
reverse(charVector);

cout << "n  "intVector":    ";
output(intVector);
cout << "n  "doubleVector": ";
output(doubleVector);
cout << "n  "stringVector": ";
output(stringVector);
cout << "n  "charVector":   ";
output(charVector);
cout << endl;


return 0;
}

I was met with these errors :

'std::vector': too many template arugments

'std::vector': no appropriate default constructor available

'x': undeclared identifier

'y': undeclared identifier

I have no idea why my x and y aren't declared. They are simply integers used to operate a for loop, and have no relation to template T at all.

I thought perhaps it had something to do with the way I wrote the reverse method, so I decided to write a reverse method that deals with a vector if ints alone to see if it worked. vector test = { 1,2,3,4,5,6,7,8,9 };

int sizeOf = test.size();    

for (int x = 0, y = sizeOf-1; x < (sizeOf / 2); x++, y--)
{
    int temp = test[x];
    test[x] = test[y];
    test[y] = temp;
}

This code worked fine. So I know it has nothing to do with how i coded my reverse algorithm.

I did some searching and found that 'std::vector': too many template arugments error often pops when you try and pass a template too many parameters. I don't believe that this is the case in my situation(but I could be very wrong, am I?)

As for the 'std::vector': no appropriate default constructor available, it looks like the usual problem in this case has to do with having a class with a default constructor being called (usually implicitly). I do not however have a class template, so I don't think(again, could be wrong. Very new) that I have any constructors that should be called at all.

Thoughts?

Have read No appropriate default constructor

and Too many template Parameter


Aucun commentaire:

Enregistrer un commentaire