0

I'm trying to write to a file but I am getting an error that I believe is because I need to overload my insertion operator. This is what I have so far

#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();
}

The line

fout << arr[row][col];

is giving me an error "no operator "<<" matches these operands

From the research I've done it seems like I have to overload that operand, but I cannot find anything about overloading something that's not a class.

Macmade
  • 52,708
  • 13
  • 106
  • 123
Dvoid
  • 1
  • Basically it is the same. Take a look at this post http://stackoverflow.com/questions/14047191/overloading-operators-in-typedef-structs-c – Javi Sep 28 '14 at 01:11
  • @JaviV That post looks completely different. – o11c Sep 28 '14 at 04:49
  • @o11c Look at my answer. In the post I told you they overload another operator with a struct exactly the same way they do with a class. That was my point. – Javi Sep 28 '14 at 04:59

2 Answers2

0

It works just like if it were a class. For instance, this is a possible implementation:

ofstream& operator<<(ofstream& os, const color& c)
{
    os << c.r << '\t' << c.g << '\t' << c.b;
    return os;
}

It works. Just adapt the os <<.... to what you need.

Javi
  • 3,440
  • 5
  • 29
  • 43
0

In C++, struct and class both declare classes. The only difference is whether their members are public or private by default (modulo compiler bugs. I have found a nasty case with the members of nested enum classes (which are not classes, despite using the class keyword!) once).

That said, it is entirely possible to overload operators where some of the arguments are non-class types (null pointers, pointers, references of either kind, unions, algebraic, member pointers, member function pointers; it is not possible to have an argument of function, array, or void type, but it is possible to have pointers or references to them (except references to void)), and in fact you're going to be overloading it for a reference, not a class, anyway. The only rule is that at least one of the arguments (including the implicit this argument for members) has to include a user-defined type somewhere (in this case, under the reference).

The signature you need is

std::ostream& operator << (std::ostream& os, const color& c) { os << "???"; return os; }

This either needs to be implemented outside the class, or inside the class as a friend if you need to access your privates (in this case you don't have any)

o11c
  • 15,265
  • 4
  • 50
  • 75