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 );
};
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.