C++ 错误:命名空间“std”中没有名为“vector”的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26563054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:37:52  来源:igfitidea点击:

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

c++c++11vector

提问by Lidia Freitas

Why this is happening?

为什么会发生这种情况?

error: no type named 'vector' in namespace 'std'; did you mean 'hecto'? void askForVector(std::vector * vector);

错误:命名空间“std”中没有名为“vector”的类型;您指的是 '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";
}

回答by Mike Seymour

vectoris a template, not a type. Either specify a particular specialisation:

vector是模板,而不是类型。要么指定一个特定的专业:

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]

回答by Marco A.

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 beforeusing the subscript operator

    std::cin >> vector[i]; // wrong
    std::cin >> (*vector)[i]; // correct
    
  1. vector 是模板,而不是类型,您需要模板参数列表,例如vector<int>在函数签名中

  2. 由于您将指针传递给向量,因此您需要使用下标运算符之前取消引用它

    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

Example