Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code in image3.cpp has the error as shown above, could you help me fix them.

ID: 3779218 • Letter: T

Question

The code in image3.cpp has the error as shown above, could you help me fix them. Thank you.

image3.cpp

#include<stdlib.h>
#include<fstream>
#include "image3.hpp"
using namespace std;

/*
ERROR CODES:
-1 : Malloc function not working properly.
-2 : Out Of Scope Pixel.
-3 : File that you are trying to open cannot be opened due to some reason.
*/

/* Constructs an image of 0x0 pixels. */
Image::Image() {
    cols=0;
    rows=0;
    pixels=NULL;
}

/* Frees all memory allocated for img */
Image::~Image() {
    cols=0;
    rows=0;
    delete pixels;
}

/* Changes the size of an image, allocating memory as necessary, and setting all pixels to fillcolor.
Returns 0 on success, or a non-zero error code.*/
int Image::resize(unsigned int width, unsigned int height, uint8_t fillcolor) {
    /*Width of the image is stored in columns.
    Height of the image is stored in rows.*/
    cols=width;
    rows=height;
    pixels=(uint8_t**)malloc(sizeof(uint8_t*)*cols);
    if(pixels==NULL) {
        return -1;
    }
    for(int i=0; i<cols; i++) {
        pixels[i]=(uint8_t*)malloc(sizeof(uint8_t)*rows);
        if(pixels[i]==NULL) {
            return -1;
        }
        for(int j=0; j<rows; j++) {
            pixels[i][j]=fillcolor;
        }
    }
    return 0;
}

/* Sets the color of the pixel at (x,y) to color. Returns 0 on success, else a non-zero error code.
If (x,y) is not a valid pixel, the call fails and the image does not change.*/
int Image::set_pixel(unsigned int x, unsigned int y,uint8_t color) {
    if(x>=cols || y>=rows) {
        return -2;
    } else {
        pixels[x][y]=color;
        return 0;
    }
}

/* Gets the color of the pixel at (x,y) and stores at the address pointed to by colorp.
Returns 0 on success, else a non-zero error code. */
int Image::get_pixel( unsigned int x, unsigned int y, uint8_t* colorp ) {
    if(x>=cols || y>=rows) {
        return -2;
    } else {
        *colorp=pixels[x][y];
        return 0;
    }
}

/* Saves the image in the file filename. In a format that can be loaded by load().
Returns 0 on success, else a non-zero error code. */
int Image::save(const char* filename) {
    ofstream OutputFile(filename);
    if(OutputFile.is_open()) {
        OutputFile<<cols<<" "<<rows<<" ";//Rows and Cols are stored first in two consecutively lines.
        for(int i=0; i<cols; i++) {
            for(int j=0; j<rows; j++) {
                if((i==(cols-1))&&(j==(rows-1))){
                    OutputFile<<unsigned(pixels[i][j]);//Does not insert a new line after the last element is inserted.
                } else {
                    OutputFile<<unsigned(pixels[i][j])<<" ";//Pixels is stored as one number per line.
                }
            }
        }
        OutputFile.close();
        return 0;
    } else {
        return -3;
    }
}

/* Load the an image from the file filename, replacing the current image size and data.
The file is in a format that was saved by save().
Returns 0 success, else a non-zero error code . */
int Image::load(const char* filename) {
    ifstream InputFile(filename);
    if(InputFile.is_open()) {
        unsigned int input;
        rows=cols=0;
        bool PixelsDeclaration=false, ColsAssignment=false, RowsAssignment=false;
        int i=0,j=0, k=1;
        while(!InputFile.eof()) {
            InputFile>>input;
            if(!ColsAssignment) {
                cols=input;
                ColsAssignment = true;
            } else if(!RowsAssignment) {
                rows=input;
                RowsAssignment = true;
            } else {
                if(!PixelsDeclaration) {
                    pixels = (uint8_t**)malloc(sizeof(uint8_t*)*cols);
                    if(pixels==NULL) {
                        return -1;
                    }
                    for(int i=0; i<cols; i++) {
                        pixels[i]=(uint8_t*)malloc(sizeof(uint8_t)*rows);
                        if(pixels[i]==NULL) {
                            return -1;
                        }
                    }
                    pixels[i][j]=input;
                    j++;
                    PixelsDeclaration=true;
                } else {
                    pixels[i][j++]=input;
                    if(j%rows == 0) {
                        j=0;
                        i++;
                    }
                }
            }
        }
        InputFile.close();
        return 0;
    } else {
        return -3;
    }
}

int main() {
}

image3.hpp(resourse)

#include <stdint.h> // for uint8_t

class Image {

public:
unsigned int cols;
unsigned int rows;
uint8_t** pixels;

/* Constructs an image of 0x0 pixels. */
Image();

/* Frees all memory allocated for img */
~Image();

/* Changes the size of an image, allocating memory as necessary, and
     setting all pixels to fillcolor. Returns 0 on success, or a
     non-zero error code.*/
int resize( unsigned int width, unsigned int height, uint8_t fillcolor );

/* Sets the color of the pixel at (x,y) to color. Returns 0 on
     success, else a non-zero error code. If (x,y) is not a valid
     pixel, the call fails and the image does not change.*/
int set_pixel( unsigned int x, unsigned int y, uint8_t color );

/* Gets the color of the pixel at (x,y) and stores at the address
     pointed to by colorp. Returns 0 on success, else a non-zero error
     code. */
int get_pixel( unsigned int x, unsigned int y, uint8_t* colorp );

/* Saves the image in the file filename. In a format that can be
     loaded by load(). Returns 0 on success, else a non-zero error
     code. */
int save( const char* filename );

/* Load the an image from the file filename, replacing the current
     image size and data. The file is in a format that was saved by
     save(). Returns 0 success, else a non-zero error code . */
int load( const char* filename );
};

Make dout cp LAB/image 3.hpp cp LAB/test 3.cpp cp REPO/image 3.cpp g++ -Wall -o t3 test 3.cpp image3.cpp Make stderr: test 3.cpp: In function a€ int main()a€"": test3.cpp 198:21: warning comparison between signed and unsigned integer expressions [-Wsign-compare] test3.cpp 199:23: warning comparison between signed and unsigned intege r expressions I-Wsign-compare] test3.cpp 222:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] image3.cpp: In member function int Image: resize (unsigned int unsigned int uint8 t)a€"": image3.cpp: 38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare image3.cpp:43:24: warning: comparison between signed and unsigned integer expressions I-Wsign-compare] image3.cpp: In member function int Image save (const char*) a€ image3.cpp:78:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare image3.cpp:79:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] image3.cpp: In member function int Image: :load (const char*)€": image3.cpp 118:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] image3.cpp: 124:28: warning: name lookup of ia€" changed lenabled by default] image 3.cpp: 103:13: warning matches this i€T under old rules lenabled by default] de image 3.cpp: 118:29: warning image3 .cpp: 103:22: warning: unused variable a€ ka€" [-Wunused-variable /tmp/ccfZ2udV.o: In function main' image3 .cpp: (.text+0x7a8) multiple definition of main /tmp/ccpXXxQE.o:test3.cpp: (.text+0x0) first defined here collect2: ld returned 1 exit status make [t3] Error 1 Build failed

Explanation / Answer

There is multiple Definition of main () { } , Empty implementation in image3.cpp
One in test3.cpp and the other in image3.cpp . Since main () of image3.cpp is empty remove that . I have pasted the same code by removing int main () { }

#include<stdlib.h>
#include<fstream>
#include "image3.hpp"
using namespace std;
/*
ERROR CODES:
-1 : Malloc function not working properly.
-2 : Out Of Scope Pixel.
-3 : File that you are trying to open cannot be opened due to some reason.
*/
/* Constructs an image of 0x0 pixels. */
Image::Image() {
cols=0;
rows=0;
pixels=NULL;
}
/* Frees all memory allocated for img */
Image::~Image() {
cols=0;
rows=0;
delete pixels;
}
/* Changes the size of an image, allocating memory as necessary, and setting all pixels to fillcolor.
Returns 0 on success, or a non-zero error code.*/
int Image::resize(unsigned int width, unsigned int height, uint8_t fillcolor) {
/*Width of the image is stored in columns.
Height of the image is stored in rows.*/
cols=width;
rows=height;
pixels=(uint8_t**)malloc(sizeof(uint8_t*)*cols);
if(pixels==NULL) {
return -1;
}
for(int i=0; i<cols; i++) {
pixels[i]=(uint8_t*)malloc(sizeof(uint8_t)*rows);
if(pixels[i]==NULL) {
return -1;
}
for(int j=0; j<rows; j++) {
pixels[i][j]=fillcolor;
}
}
return 0;
}
/* Sets the color of the pixel at (x,y) to color. Returns 0 on success, else a non-zero error code.
If (x,y) is not a valid pixel, the call fails and the image does not change.*/
int Image::set_pixel(unsigned int x, unsigned int y,uint8_t color) {
if(x>=cols || y>=rows) {
return -2;
} else {
pixels[x][y]=color;
return 0;
}
}
/* Gets the color of the pixel at (x,y) and stores at the address pointed to by colorp.
Returns 0 on success, else a non-zero error code. */
int Image::get_pixel( unsigned int x, unsigned int y, uint8_t* colorp ) {
if(x>=cols || y>=rows) {
return -2;
} else {
*colorp=pixels[x][y];
return 0;
}
}
/* Saves the image in the file filename. In a format that can be loaded by load().
Returns 0 on success, else a non-zero error code. */
int Image::save(const char* filename) {
ofstream OutputFile(filename);
if(OutputFile.is_open()) {
OutputFile<<cols<<" "<<rows<<" ";//Rows and Cols are stored first in two consecutively lines.
for(int i=0; i<cols; i++) {
for(int j=0; j<rows; j++) {
if((i==(cols-1))&&(j==(rows-1))){
OutputFile<<unsigned(pixels[i][j]);//Does not insert a new line after the last element is inserted.
} else {
OutputFile<<unsigned(pixels[i][j])<<" ";//Pixels is stored as one number per line.
}
}
}
OutputFile.close();
return 0;
} else {
return -3;
}
}
/* Load the an image from the file filename, replacing the current image size and data.
The file is in a format that was saved by save().
Returns 0 success, else a non-zero error code . */
int Image::load(const char* filename) {
ifstream InputFile(filename);
if(InputFile.is_open()) {
unsigned int input;
rows=cols=0;
bool PixelsDeclaration=false, ColsAssignment=false, RowsAssignment=false;
int i=0,j=0, k=1;
while(!InputFile.eof()) {
InputFile>>input;
if(!ColsAssignment) {
cols=input;
ColsAssignment = true;
} else if(!RowsAssignment) {
rows=input;
RowsAssignment = true;
} else {
if(!PixelsDeclaration) {
pixels = (uint8_t**)malloc(sizeof(uint8_t*)*cols);
if(pixels==NULL) {
return -1;
}
for(int i=0; i<cols; i++) {
pixels[i]=(uint8_t*)malloc(sizeof(uint8_t)*rows);
if(pixels[i]==NULL) {
return -1;
}
}
pixels[i][j]=input;
j++;
PixelsDeclaration=true;
} else {
pixels[i][j++]=input;
if(j%rows == 0) {
j=0;
i++;
}
}
}
}
InputFile.close();
return 0;
} else {
return -3;
}
}

Others are mere warning , we can ignore that, your makefile should compile successfully.
Thanks, I hope it clarifies, let me know if there is something.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote