简体   繁体   English

C ++错误:类型返回的值没有可行的转换

[英]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/returnlist/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.

相关问题 c ++ list,没有&#39;value_type&#39;的可行转换 - c++ list, no viable conversion from 'value_type' C++ 代码错误:从“int”类型的返回值到 function 返回类型“std::string”(又名“basic_string”)没有可行的转换<char> ')</char> - C++ Code Error: no viable conversion from returned value of type 'int' to function return type 'std::string' (aka 'basic_string<char>') C ++错误:没有从&#39;mapped_type&#39;到&#39;int&#39;的可行转换 - C++ error: no viable conversion from 'mapped_type' to 'int' 错误“无法从“int[2]”类型的返回值转换为函数返回类型“vector”<int> &#39;&quot; - Error "No Viable Conversion From Returned Value of Type 'int[2]' to function return type 'vector<int>'" 没有从const_iterator类型的返回值到迭代器的可行转换 - no viable conversion from returned value of type const_iterator to iterator 试图返回一个带索引的数组,但收到错误,没有从返回类型“int [2]”的返回值到 function 返回类型“vec”的可行转换 - Trying to return an array with indices but receiving error no viable conversion from returned value of type 'int [2]' to function return type 'vec C++ 模板 - 没有可行的转换错误 - C++ Template - No viable conversion error C++ 继承“无可行转换”错误 - C++ Inheritance "No Viable Conversion" Error 没有从“int”类型的返回值到函数返回类型(向量)的可行转换 - No viable conversion from returned value of type 'int' to function return type (vectors) 没有从 bankAccount 类型的返回值到 function 返回类型 int 的可行转换 - no viable conversion from returned value of type bankAccount to function return type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM