我試圖寫入一個文件,但我得到一個錯誤,我相信是因爲我需要重載我的插入操作符。這是我迄今爲止重載插入操作
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct color
{
unsigned char r;
unsigned char g;
unsigned char b;
};
void initialize(color arr[][600], int nrows, int ncols);
void writeAll(color arr[][600], int nrows, int ncols);
const int NROWS = 400;
const int NCOLS = 600;
int main()
{
color arr[400][600];
initialize(arr, 400, 600);
writeAll(arr, 400, 600);
return 0;
}
// Background
void initialize(color arr[][NCOLS], int nrows, int ncols)
{
for (int row = 0; row < NROWS/2; row++)
{
for (int col = 0; col < NCOLS/2; col++)
{
arr[row][col].r = 255;
arr[row][col].g = 255;
arr[row][col].b = 255;
}
}
}
void writeAll(color arr[][600], int nrows, int ncols)
{
ofstream fout("out.ppm", ios::out | ios::binary);
fout << "P6" << endl;
fout << ncols << " " << nrows << endl;
fout << 255 << endl;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < ncols; col++)
{
fout << arr[row][col];
}
}
fout.close();
}
線
fout << arr[row][col];
是給我一個錯誤「沒有運營商」 < <「匹配這些操作數
從研究,我已經做到了好像我必須重載那個操作數,但我找不到任何有關重載不是類的東西。
基本上是一樣的。看看這篇文章http://stackoverflow.com/questions/14047191/overloading-operators-in-typedef-structs-c – 2014-09-28 01:11:28
@JaviV這篇文章看起來完全不同。 – o11c 2014-09-28 04:49:29
@ o11c看看我的答案。在這篇文章中,我告訴過你,他們用一個結構完全重寫另一個運算符,就像他們對一個類所做的一樣。這是我的觀點。 – 2014-09-28 04:59:30