Feelsbadman
Feelsbadman

Reputation: 1165

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:

enter image description here

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

}

Upvotes: 2

Views: 15066

Answers (2)

DanielCollier
DanielCollier

Reputation: 765

void printVector(vector<int> const & vec_name)
{
  for(size_t i = 0; i<vec_name.size(); i++){
    cout << vec_name[i] << " ";
  }
    cout << "\n";
}

Upvotes: 1

Feelsbadman
Feelsbadman

Reputation: 1165

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

}

Upvotes: 2

Related Questions