Passing Vector as a Reference
I am having a problem passing a vector as a reference parameter and can not figure out why the program is not compiling or working. It is meant to read in text from a file, manipulate the vector into a new one, then print out the new one to a file. Everything is working except for the method that manipulates the vector. The syntax seems correct from reading several websites which is what confuses me. I have a header file but there is nothing wrong with it. Any hints are greatly appreciated!
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include "hw1.h"
using namespace std;
int main() {
int x;
vector<int> first;
vector<int> second;
ifstream inFile;
inFile.open("input.txt");
while (inFile >> x) {
first.push_back(x);
}
inFile.close();
method(first, second);
ofstream outFile;
outFile.open("output.txt");
for (int i = 0; i < second.size(); i++) {
outFile << second[i] << endl;
}
outFile.close();
return 0;
}
void method(vector<int>& x, vector<int>& y) {
int step = 0;
int j = 1;
vector<int> temp(x.size() / 2);
for (int i = 0; i < x.size() / 2; i++) {
y[i] = x[step] + x[j];
j+= 2;
step+= 2;
}
}