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

莉迪亚·弗雷塔斯(Lidia Freitas)

为什么会这样呢?

错误:名称空间“ std”中没有名为“ vector”的类型;你是说“ hecto”吗?无效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";
}
迈克·西摩

vector是模板,而不是类型。指定特定的专业化:

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

或使函数通用

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

使用引用而不是指针可能会更好:

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

或按值返回向量:

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

避免像

std::cin >> vector[i]; // should be (*vector)[i]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

名称空间“ std”中没有名为“ nullptr_t”的类型

Android NDK-名称空间“ std”中没有名为“ function”的类型

在LLVM / Clang下编译时,名称空间“ std”中没有名为“ unique_ptr”的类型

错误:“类std :: result_of”中没有名为“ type”的类型

grpc和protobuf-错误:std :: result_of <>中没有名为'type'的类型

在命名空间 boost 中没有名为 asio 的类型

错误:名称空间“ std”中没有名为“ stod”的成员

向量查找错误:'struct std :: iterator_traits中没有名为'iterator_category'的类型

错误:名称空间“ std”中没有名为“ enable_if_t”的模板;你是说'enable_if'吗?

错误:名称空间“ std”中没有名为“ is_same_v”的模板;你是说'is_same'吗?

错误:在“class date::year_month_day”中没有名为“_traits”的类型?

Boost Spirit X3:错误:“...”中没有名为“size”的类型

名称空间“ namespace”中没有名为“ name”的成员

C++ 线程错误:没有名为“type”的类型

错误:名称空间“ System.Web”中不存在类型或名称空间名称“ Configuration”

在“ std :: result_of”中没有名为“ type”的类型;从重载函数获取返回类型

“Android Studio 中命名空间‘std’中没有名为‘variant’的模板”

类std :: vector没有名为的成员

命名空间“std”中没有名为“hardware_constructive_interference_size”的成员

在类std :: result_of中没有名为“ type”的类型

C ++在'struct std :: iterator_traits <int>'中没有名为'value_type'的类型

错误:名称空间“ pqxx”中的“行”未命名类型

安装npm软件包时出现“在名称空间'v8'中没有名为'WeakCallbackData'的模板”错误

_ViewImports.cshtml中的错误在名称空间“ yyy”中不存在类型或名称空间名称“ xxx”

Microsoft Edge WebView2-错误:名称空间“ Microsoft”中不存在类型或名称空间名称“ Web”

Devops:错误CS0234:类型或名称空间名称'ApplicationModel'在名称空间'Windows'中不存在

c#引用错误-名称空间'EntityFramework'中不存在类型或名称空间名称'Extended'

我在 C# 中收到错误“命名空间中不存在类型或命名空间名称”

C++ 线程错误:没有名为 'type' MINGW 的类型