Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

vector

Why this is happening?

error: no type named 'vector' in 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";
}
like image 494
Lidia Freitas Avatar asked Oct 25 '14 13:10

Lidia Freitas


People also ask

Is vector in std namespace?

Vectors in C++C++ has a vector class within the std namespace. A vector is similar to an array, in a sense where a series of elements are stored with the same variable name. Unlike arrays, vectors are dynamically sized, which is a major advantage.

Did you mean std :: vector?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.


2 Answers

vector is a template, not a type. Either specify a particular specialisation:

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

or make the function generic

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

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

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

or returning the vector by value:

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

to avoid errors like

std::cin >> vector[i]; // should be (*vector)[i]
like image 170
Mike Seymour Avatar answered Sep 21 '22 16:09

Mike Seymour


There are multiple issues:

  1. vector is a template, not a type, you need the template argument list e.g. vector<int> in the function signature

  2. Since you're passing a pointer to a vector you need to dereference it before using the subscript operator

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

The following could 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

like image 40
Marco A. Avatar answered Sep 20 '22 16:09

Marco A.