-1

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

Community
  • 1
  • 1
Podo
  • 709
  • 9
  • 30
  • Voted to close as lacking an example that reproduces the problem. Just in case you're not aware, there is a [`std::reverse`](http://en.cppreference.com/w/cpp/algorithm/reverse) function. – Cheers and hth. - Alf Jun 10 '16 at 23:18
  • Which example would you like? My entire solution? It won't even compile..How can I provide what you are looking for? – Podo Jun 10 '16 at 23:19
  • No-one can guess what the code that causes the problem, looks like. You have to show it if you want any answer. Try to **reduce** it to a minimal example, but still complete so that readers can just copy and paste it to reproduce the error. – Cheers and hth. - Alf Jun 10 '16 at 23:20
  • Alright, I'll edit it then. Give me a minute. – Podo Jun 10 '16 at 23:21
  • @Cheersandhth.-Alf, should be able to just copy and paste now. Let me know if it doesn't work. – Podo Jun 10 '16 at 23:29
  • @Cheersandhth.-Alf - I am very much aware of the reverse function. Thank you though! Looking to try and code it this way :) – Podo Jun 10 '16 at 23:32
  • I need #include for reverse though, don't I? Just double checking. – Podo Jun 10 '16 at 23:32
  • Yes. And you need to include `` to use `std::vector`, and so on. **Tip::**: You absolutely don't need the Microsoft monstrosity ``, and that can prevent some readers (who don't know that) from trying your code. Just turn off "precompiled headers" in the project settings, and remove that include. – Cheers and hth. - Alf Jun 10 '16 at 23:40
  • OK, I retracted the close vote and answered the question. But since I didn't get the errors you mention it may be that the example code you posted now wasn't properly illustrating the original problem. – Cheers and hth. - Alf Jun 10 '16 at 23:41

1 Answers1

1

I don't get your exact errors, but I've marked relevant fixes below.

I ran the code through AStyle to fix the formatting.

//#include "stdafx.h"
#include <iostream>

#include <vector>           //!FIX
#include <string>           //!FIX

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;
        return;     //!FIX
    }

    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 think this should probably better have been posted to the code review site.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Is this a more appropriate post for code review? I'm not a member, and haven't ever ventured that way. I'll look at your post, and keep crackin at it. If I can't find anything I'll send it that way. Thank you. – Podo Jun 10 '16 at 23:40
  • This is a more appropriate answer for CR, but the original question is off-topic because they are asking about non-working code. –  Jun 10 '16 at 23:43
  • @Hosch250, since when is non-working code actually off topic for stackoverflow? Nearly every question on here is about non-working code. That's a silly thing to say. I understand now that CR is a better place for this, but I have seen hundreds and hundreds of people posting bits of code that don't work looking for an answer right here on S.O. – Podo Jun 11 '16 at 00:37
  • 1
    @JeffreyDilley I didn't mean this was off-topic on SO, I meant that broken code is off-topic on CR. CR is not a better place for this question. –  Jun 11 '16 at 01:28