0
我有一個代碼段,它將零加載到表示位矩陣的向量中。當程序運行並嘗試將結果寫入輸出文件時,我會遇到seg錯誤。當程序沒有寫入輸出文件時,程序運行正常。用位矩陣加載向量
[code]
Bitmatrix::Bitmatrix(int rows, int cols)
{
int count = 0; // count variable
int count2 = 0; // 2nd count variable
if(rows <= 0 || cols <= 0)
{
fprintf(stderr, "Value of rows or columns is less than or equal to zero\n"); // print error message
M.resize(1); // resize to 1 x 1 matrix
M[0] = '0'; // set 0 as the value
}
else
M.resize(rows); // resize matrix to number of rows
for(count = 0; count < M.size(); count++)
{
for(count2 = 0; count2 < cols; count2++)
{
M[count].push_back('0'); // fill matrix with zeros
}
}
}[/code]
,在輸出文件打印功能是:
[code]void Bitmatrix::Write(string fn)
{
ofstream out; // output stream object
int count = 0; // count variable
int count2 = 0; // 2nd count variable
out.open(fn.c_str()); // open output file
for(count = 0; count < M.size(); count++)
{
for(count2 = 0; count2 < M[count].size(); count++)
{
out << M[count][count2];
}
out << endl;
}
}[/code]
有人能看到爲什麼會這樣?
謝謝。我總是忽略這樣的事情。 – miamidawgs
如果我的答案對您有幫助,請將其標記爲已接受。 –