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*>)
Fixes:
vector<S*>but you obviously want it to takevector<S>.std::vector<int>. Since anintis not anS*for any type ofS, the template did not apply.Awhen you intended to use only the first declaration.c[i]*itLess critical fixes:
using namespace std;std::endlwhen you mean"\n".