How do you output an array of structs

My question is as the title states, I am having trouble trying to output an array of structs to a file. The program is suppose to create a ppm image with a small rectangle with a different color in the middle. However, when I try to output the array to the file I get an error saying "invalid operands to binary expression". I'm at a loss at how to fix the issue. If I change the file extension to .txt the output file should resemble something like this:

P3
15 10
255
0 0 0 100 25 45 0 0 0
0 0 0 100 25 45 0 0 0
0 0 0 100 25 45 0 0 0

The groups of three should each represent a red, green, and blue pixel value. Any help on outputting the array of structs would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int IMAGE_WIDTH = 15;
const int IMAGE_HEIGHT = 10;
const int MAX_VALUE = 255;
const string FILE_NAME = "color_image.ppm";

struct ColorValues {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

void createImage(ColorValues array[][IMAGE_WIDTH], int height);
bool writeImage(ColorValues array[][IMAGE_WIDTH], int height, const string fileName );


int main() {

    ColorValues imageArray[IMAGE_HEIGHT][IMAGE_WIDTH] = {};

    createImage(imageArray, IMAGE_HEIGHT);
    writeImage(imageArray, IMAGE_HEIGHT, FILE_NAME);
    
    return 0;
}

//draws a rectangle in the array
void drawRect(ColorValues imageArray[][IMAGE_WIDTH], int imgHeight, 
    int rectTop, int rectLeft, int rectHeight, int rectWidth, 
    int redLevel, int greenLevel, int blueLevel){
    
    ColorValues value;
    value.red = redLevel;
    value.green = greenLevel;
    value.blue = blueLevel;

    for(int rowIndex = rectTop; rowIndex < rectTop + rectHeight; rowIndex++)
        for(int colIndex = rectLeft; colIndex < rectLeft + rectWidth; colIndex++){
            imageArray[rowIndex][colIndex] = value;
            
        }
}

//adds shapes to the image array
void createImage(ColorValues imageArray[][IMAGE_WIDTH], int height){

    drawRect(imageArray, height, 2, 2, 5, 7, 240, 60, 100);

}

//outputs the array to a file
bool writeImage(ColorValues imageArray[][IMAGE_WIDTH], int height, const string fileName ){
    
    ofstream outputFile;
    outputFile.open(fileName);
    
    if (! outputFile) { 
        cout << "File " << fileName << " could not be opened.\n"; 
        cout << "program terminated.\n"; 
        return false; }
    
    outputFile << "P3 \n";
	outputFile << IMAGE_WIDTH << " " << IMAGE_HEIGHT << "\n";
	outputFile << MAX_VALUE << "\n";

    for(int arrayRow = 0; arrayRow < IMAGE_HEIGHT; arrayRow++){
        for(int arrayColumn = 0; arrayColumn < IMAGE_WIDTH; arrayColumn++){
            outputFile <<  imageArray[arrayRow][arrayColumn] << " ";
        }
        outputFile << endl;
    }

    outputFile.close();
        
    return true;
    
}
Either you have to output each .member yourself.

Or you can add an operator<< to your struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ cat foo.cpp
#include <iostream>
using namespace std;

struct ColorValues {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    friend ostream& operator<<(ostream& os, const ColorValues& cv) {
        os << cv.red << "," << cv.green << "," << cv.blue;
        return os;
    }
};

int main ( ) {
    ColorValues cv = {65,70,75};
    cout << cv.red << " " << cv.green << " " << cv.blue << endl;
    cout << cv << endl;
}
$ g++ -Wall -pedantic foo.cpp
$ ./a.out 
A F K
A,F,K

I figured out a fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
bool writeImage(ColorValues imageArray[][IMAGE_WIDTH], int height, const string fileName ){
    
    ofstream outputFile;
    outputFile.open(fileName);
    
    if (! outputFile) { 
        cout << "File " << fileName << " could not be opened.\n"; 
        cout << "program terminated.\n"; 
        return false; }
    
    outputFile << "P3 \n";
	outputFile << IMAGE_WIDTH << " " << IMAGE_HEIGHT << "\n";
	outputFile << MAX_VALUE << "\n";

    for(int arrayRow = 0; arrayRow < IMAGE_HEIGHT; arrayRow++){
        for(int arrayColumn = 0; arrayColumn < IMAGE_WIDTH; arrayColumn++){
            outputFile << setw(5) << (short) imageArray[arrayRow][arrayColumn].red 
            << setw(4) << (short) imageArray[arrayRow][arrayColumn].green 
            << setw(4) << (short) imageArray[arrayRow][arrayColumn].blue;
        }
        outputFile << endl;
    }

    outputFile.close();
        
    return true;
    
}
Topic archived. No new replies allowed.