[英]C++ Error: no viable conversion from returned value of type
I am trying to pass a vector to a function as an argument/parameter
in order to print/return
contents of that list/array/vector
but when I compile the code I'm facing this error: 我试图将向量作为argument/parameter
传递给函数,以便print/return
该list/array/vector
内容,但是当我编译代码时,我遇到了这个错误:
here's the code: 这是代码:
#include <iostream>
#include <vector>
using namespace std;
int printVector(vector<int> vec_name){
return copy(vec_name.begin(), vec_name.end(), ostream_iterator<int>(cout," ")); // returning contents of the array/vector
}
int main(){
vector<int> array;
for(int i=0;i<=10;i++){
array.push_back(i); // inserting values to the array
}
printVector(array); // Printing the vector array
}
PROBLEM SOLVED: 问题解决了:
used for loop in order to print each value from the vector: 用于循环以打印向量中的每个值:
void printVector(vector<int> &vec_name){
for(int i=0; i<vec_name.size(); i++){
cout << vec_name[i] << " ";
}
}
void printVector(vector<int> const & vec_name)
{
for(size_t i = 0; i<vec_name.size(); i++){
cout << vec_name[i] << " ";
}
cout << "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.