gpt4 book ai didi

c++ - 错误 : no type named 'vector' in namespace 'std'

转载 作者:可可西里 更新时间:2023-11-01 15:48:38 24 4
gpt4 key购买 nike

为什么会这样?

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";
}

最佳答案

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

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

或者使函数通用

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

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

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

或按值返回 vector :

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

避免类似的错误

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

关于c++ - 错误 : no type named 'vector' in namespace 'std' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com