Here are a few snippets of my code. First are my functions that I wrote. The one giving me an error is the fileout function, which is supposed to write the vector to a file.
vector<int> random(int rowsize, int columnsize) // generates a random matrix given number of rows and columns, stores it as a 1-dimensional vector
{
int elements = rowsize*columnsize;
vector<int> x(elements);
int matrix[10][10];
int n=0;
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
matrix[i][j]= -10 + rand() % 21;
x[n++] = matrix[i][j];
}
}
return x;
}
void print(vector<int> matrix, int columnsize) //prints the matrix given the number of columns
{
for(int i=0; i < matrix.size(); i++)
{
if(i%columnsize != 0)
cout << setw(10) << matrix[i];
if(i%columnsize == 0)
cout << endl << setw(10) << matrix[i];
}
cout << endl;
}
vector<int> addmat(vector<int> matrixA, vector <int> matrixB) //adds two matrices together, takes vector input
{
vector<int> matrix(matrixA.size());
for(int i=0; i< matrixA.size(); i++)
matrix[i] = (matrixA[i] + matrixB[i]);
return matrix;
}
vector<int> filefill(ifstream &fin, string file, int rowsize, int columnsize) //creates a matrix from a file
{
fin.open(file.c_str());
vector<int> matrix;
int number;
for(int i=0; i < rowsize*columnsize; i++)
{
while(fin >> number)
matrix.push_back(number);
}
fin.close();
return matrix;
}
void fileout(ofstream fout, string file, vector<int> matrix) //writes a matrix onto a file
{
fout.open(file.c_str());
for(int i=0; i < matrix.size(); i++)
fout << matrix[i];
fout.close();
}
This is the use of the function in the main. The fout inside the fileout function is marked as an error:
"6 IntelliSense: "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &_Right) [with _Elem=char, _Traits=std::char_traits]" (declared at line 1034 of "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\fstream") is inaccessible c:\Users\William\Desktop\C++\Homework8\HW8\hw8.cpp 163 11 HW8
else if (answer == "N" || answer == "n")
{
matrixA = random (Arow, Acolumn);
cout << "Matrix A" << endl;
print(matrixA, Acolumn);
matrixB = random (Brow, Bcolumn);
cout << "Matrix B" << endl;
print(matrixB, Bcolumn);
cout << endl << "Matrix A + Matrix B =" << endl;
vector<int> matrixC = addmat (matrixA, matrixB);
print(matrixC, Acolumn);
for (int i = 0; i < matrixA.size(); i++)
{
matrixB.push_back(matrixA[i]);
}
string filo;
cin >> filo;
ofstream fout;
fileout(fout, filo, matrixB);
}
Not sure what the problem is????????????
filefill, where you take the stream by reference andfileout, where you take the stream by value. Streams aren't value types meant to be copied around (and passing them by value to a function implies a copy of the value), they are meant to be passed by reference. - legalize