0

I have to create a function that takes in a vector of unknown item types. Here is my code:

template <typename S>
void printVector(vector<S*> c){
   typename vector<S>::const_iterator A = c.begin();
   for (int A; A != c.end(); A++){
       cout<<c[A]<<" ";
   }
   cout<<endl;
}

In my main class here is my vector and function call:

vector<int> x;
int j=5;
for(int i=0;i<j;i++){
    x.push_back(num[i]);
}
printVector(x);

When I try to compile this code I get these errors:

exercise1_1.cpp: In function ‘int main()’:
exercise1_1.cpp:33:15: error: no matching function for call to ‘printVector(std::vector<int>&)
exercise1_1.cpp:33:15: note: candidate is:
exercise1_1.cpp:13:7: note: template<class S> void printVector(std::vector<S*>)

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
user1672267
  • 175
  • 1
  • 2
  • 10
  • 3
    A vector of 'S' or a vector of pointer to 'S'? What type is 'std::vector<>::end()'? Is that comparable with 'int' as in 'A != c.end()'? – David Rodríguez - dribeas Nov 08 '12 at 22:55
  • 6
    Do me a favour; mouse over the homework tag. – chris Nov 08 '12 at 22:55
  • sorry @chris I'm not as familiar with this site as you but okay.. It is a vector of type 'S' and end() returns an iterator referring to the end element in the vector container. – user1672267 Nov 08 '12 at 23:00
  • 1
    @user1672267: Then `vector` is a typo and so is `int A`. In fact I think you just wrote `int A` because you're used to seeing `int` declarations in that part of a `for` construct, but it's not correct in this case. – Lightness Races in Orbit Nov 08 '12 at 23:01

2 Answers2

3

There were some errors in your code. Let's look at them:

  • In the signature of printVector, you take a parameter of type vector<S*>. This means that, for a given type S, you take a vector of pointers to S. So, for example, if the type is int, you should 'send' a vector<int*>. In your example, you are attempting to 'send' a vector<int>, so the signature of printVector should look like this:

    template <typename S>
    void printVector(vector<S> c)
    
  • In your for loop, you are declaring the variable A again, as int. In the C++ standard library, you can access every item in a vector using iterators or just accessing them like an array. You were mixing both things.

If you want to loop using iterators, it should look like this:

typename vector<S>::const_iterator a = c.begin();
for (; a != c.end(); a++){
    cout<<*a<<" ";
}

Which means, for every item in the vector, print its value (notice I used *a because I'm using iterators).

Or using the index-based approach:

for (int i = 0; i < c.size(); i++){
   cout<<c[i]<<" ";
}

Also, as a common practice in C++ community, use a const reference when receiving variables of non-primitive types. This prevents that a copy of the object is performed, so it will probably be faster when dealing with huge collections. Example:

 void printVector(const vector<S>& c)

So here is a full working example:

#include <vector>
#include <iostream>

using namespace std;

template <typename S>
void printVector(const vector<S>& c){
   for (int i = 0; i < c.size(); i++){
       cout<<c[i]<<" ";
   }
   cout<<endl;
}

int main(int argc, char** args) {
    vector<int> x;
    int j=5;
    for(int i=0;i<j;i++){
        x.push_back(i);
    }
    printVector<int>(x); // Can be printVector(x); too
    return 0;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Daniel Castro
  • 1,290
  • 2
  • 11
  • 22
  • Why would the compiler fail to deduce the template arguments in this case? `printVector(x)` should be perfectly fine – Grizzly Nov 08 '12 at 23:16
  • I just wasn't sure if the C++ compilers actually infer the type.. But as they do, I just edited it. Thanks for the comment – Daniel Castro Nov 08 '12 at 23:19
2
template <typename S>
void  printVector(const std::vector<S>& c){
   typename std::vector<S>::const_iterator A = c.begin();
   for (; A != c.end(); A++){
       std::cout<<*A<<" ";
   }
   std::cout<<"\n";
}

Fixes:

  • Your function was declared to take a vector<S*> but you obviously want it to take vector<S>.
    • Your main function invoked print_vector with an argument of std::vector<int>. Since an int is not an S* for any type of S, the template did not apply.
  • You have redeclared A when you intended to use only the first declaration.
  • You conflated indexed access to the vector with access through the iterator.
    • If you have an int: c[i]
    • If you have an iterator: *it

Less critical fixes:

  • Avoid using namespace std;
  • Never use std::endl when you mean "\n".
  • You should pass the parameter by const reference, not by value
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Rob, the reason the `homework` tag is obsolete is that Stack Overflow encourages **all** answers to consist of dialog and explanation, rather than spoonfeeding code dumps. – Lightness Races in Orbit Nov 08 '12 at 23:03
  • @LightnessRacesinOrbit - right. That's the point of the dialog that follows the code dump. – Robᵩ Nov 08 '12 at 23:04
  • It wasn't there to begin with, and you know it ;) * > FGIW – Lightness Races in Orbit Nov 08 '12 at 23:08
  • Since you're streaming only `\n`, why not `std::cout << '\n';`? – Lightness Races in Orbit Nov 08 '12 at 23:08
  • Yeah, I enter the answer incrementally and save my work often. I need to rethink that strategy. – Robᵩ Nov 08 '12 at 23:09
  • "*why not `'\n'`*?" - No good reason. My fingers know `"` better than `'`. – Robᵩ Nov 08 '12 at 23:10
  • 1
    Why would the OP not mean `std::endl`? Flushing the output at the end of the line seems like a sensible think to do here. – Grizzly Nov 08 '12 at 23:13
  • 1
    @Grizzly - Either the OP directed the output to an interactive console or he directed to a file. If it is a console, the runtime will automatically flush the output at the end of the line, and he gains nothing. If it is a file it doesn't matter if it is flushed (unless his program crashes), and he gains nothing and loses performance. *Maybe* OP meant to use `endl`. In my experience, 99% of people who use `endl` don't require or intend the flush. – Robᵩ Nov 08 '12 at 23:16
  • @Grizzly : See also this question: [What is the C++ iostream endl fiasco?](http://stackoverflow.com/q/5492380/636019) – ildjarn Nov 08 '12 at 23:24