Function print with error


I made a program that calculates the Fibonacci numbers in X and Y using Z as a helper.

When I pass the Fibonacci function vector to the imprime function, it does not print. I made a test by putting cout inside the for of the calculation and I noticed that the error is precisely in the imprime function, since cout in the calculation does everything exactly as it had to do. I suspect that the problem is exactly in for of imprime .

I think this information will help: my imprime function generates an error that I can not solve:


C4018 '

asked by anonymous 02.05.2016 / 21:23

2 answers


You would need to pass the vector always by reference, as you did correctly in the imprime() function. It seems that the error is in it, but in fact it occurred before and the vector is not correctly filled as expected. I would simplify the code like this:

#include <iostream>
#include <vector>
using namespace std;

void imprime(vector<int>& v) {
    for(int x = 0; x < v.size(); x++)
        cout << v[x] << " ";

void fibonacci(int x, int y, vector<int>& fi) {
    int z = 0;
    for (int b = 0; b < fi.size(); b++) {
        z = x + y;
        fi[b] = z;
        x = y;
        y = z;      

int main() {
    int valor1, valor2, numeroElementos;

    while (cin >> valor1 >> valor2 >> numeroElementos) {
        vector<int> numeros(numeroElementos);
        fibonacci(valor1, valor2, numeros);
    return 0;

See running on ideone .

02.05.2016 / 22:37

Change this:

void fibonacci(int x, int y, std::vector<int> fi, int numeroElementos);


void fibonacci(int x, int y, std::vector<int>& fi, int numeroElementos);

And make the same change in function definition. Also, declare the vector numeros within while , look like this:

while (std::cin >> valor1 >> valor2 >> NumeroElementos) {
    std::vector<int> numeros;
    fibonacci(valor1, valor2, numeros, NumeroElementos);
02.05.2016 / 22:10