简体   繁体   English

声明一个向量返回类型函数并返回向量

[英]declare a vector return-type function and return the vector

I want to write a function that has a return-type as a vector according to codecadmey c++ course I can set the return type as an int,bool,std::string,std::vector to name some but when I try to declare a function that returns a vector I get lots of errors including.我想根据 codecadmey c++ 课程编写一个具有返回类型作为向量的函数我可以将返回类型设置为 int,bool,std::string,std::vector 来命名一些但是当我尝试声明时一个返回向量的函数我得到了很多错误,包括。

1 - invalid use of template-name 'std::vector' 2 - 'make_arr' was not declared in this scope 1 - 模板名称 'std::vector' 的无效使用 2 - 'make_arr' 未在此范围内声明

#include <iostream>
#include <vector>

using namespace std;


std::vector make_arr(int num ){
    std::vector<int> arr(num);
    for (int i=0; i < num; i++){
        cin >> arr[i];
    }

    for (int i=0; i < arr.size(); i++){
        cout << arr[i];
    }
}





int main(){
    int num=0;
    cin >> num;
    make_arr(num);
    cout << "done\n";
}
std::vector<int> make_arr(int num ){

This fixes the first issue which is causing the second issue.这解决了导致第二个问题的第一个问题。 You have a 3rd issue that you never actually return the vector, which you just need to do by doing你有第三个问题,你从来没有真正返回向量,你只需要这样做

return arr;

Then you should do something with the returned vector, but that won't cause any compilation issues if not然后你应该对返回的向量做一些事情,但如果没有,那不会导致任何编译问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM