Error: no type named 'vector' in namespace 'std'

Why is this happening?

error: there is no name with the name 'vector' in the namespace 'std'; did you mean hecto? void askForVector (std :: vector * vector);

#include <iostream> #include <vector> void askForVector(std::vector * vector); int main() { std::vector<int> vector; int size; askForVector(&vector); std::cout << "\nsize: " << vector.size() << std::endl; std::cout << vector.at(0); } void askForVector(std::vector * vector) { int size; std::cout << "please insert the size of vector to order: "; std::cin >> size; vector->resize(size); for(int i = 0; i<size; i++){ std::cout << "please insert a value for the " << i+1 << " position: " ; std::cin >> vector[i]; } for(int j: *vector) std::cout << ":"<<j; std::cout << ":\n"; } 
+7
c ++ vector c ++ 11
source share
2 answers

vector is a template, not a type. Define your specific specialization:

 void askForVector(std::vector<int> * vector); 

or make a generic function

 template <typename T> void askForVector(std::vector<T> * vector); 

You might be better off using a link rather than a pointer:

 void askForVector(std::vector<int> & vector); 

or returning a vector by value:

 std::vector<int> askForVector() { std::vector<int> vector; // your code here return vector; } 

to avoid mistakes like

 std::cin >> vector[i]; // should be (*vector)[i] 
+11
source share

There are several problems:

  • vector is a template, not a type, you need a list of template arguments, for example. vector<int> in the function signature

  • Since you are passing a pointer to a vector, you need to dereference it before using the index operator

     std::cin >> vector[i]; // wrong std::cin >> (*vector)[i]; // correct 

The following may work:

 #include <iostream> #include <vector> void askForVector(std::vector<int> * vector); int main() { std::vector<int> vector; int size; askForVector(&vector); std::cout << "\nsize: " << vector.size() << std::endl; std::cout << vector.at(0); } void askForVector(std::vector<int> * vector) { int size; std::cout << "please insert the size of vector to order: "; std::cin >> size; vector->resize(size); for (int i = 0; i<size; i++){ std::cout << "please insert a value for the " << i + 1 << " position: "; std::cin >> (*vector)[i]; } for (int j : *vector) std::cout << ":" << j; std::cout << ":\n"; } 

Example

+4
source share

All Articles