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

#include <iostream> #include <fstream> #include <string> #include <cstdlib> usin

ID: 2246496 • Letter: #

Question

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;
const int SIZE = 150;


class color
{
protected:
int a[SIZE][SIZE*3];
int m;
int n;

public:
color(unsigned char, unsigned char, unsigned char);
unsigned char r;
unsigned char g;
unsigned char b;

int Width = 240;
int height = 320;

int getM()const;
void setM(int m);
int getN()const;
void setN(int n);
void writeppm(string);
void createpicture();


};
int color::getM()const{
return m;
}
void color::setM(int M){
this->m=m;
}
int color::getN()const{
return n;
}
void color::setN(int n){
this->n=n;
}
color::color(unsigned char, unsigned char, unsigned char){
r = 0;
g = 0;
b = 0;
}
void color::writeppm(string filename){

ofstream File(filename.c_str());
int amount = 255;
File << "P3" << endl;
File << "Created by " << endl;
File << Width << " " << height << endl;
File << amount << endl;
File << " " << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n*3; j++)
File << a[i][j]<< endl;
File << endl;
}
File << endl;


}
void color::createpicture(){
color grey(127,127,127);

for(int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j*3]=grey.r;
a[i][j*3+1]=grey.g;
a[i][j*3+2]=grey.b;
}
}
}


int main()
{
color X;
X.createpicture();
X.writeppm("image1.ppm");
cout << "Image has been created!!" << endl;

return 0;
}

Help - getting error on line 92 no matching function for call to color::color() / 49 candidate expects 3 arguments/ 10 constexpr color::color(const color&)

C++

Explanation / Answer

The errors shown were regarding the initialization of member variables Width and height, this has been corrected by declaring then "static const" , The other error was regarding "no matching function for call to color::color()", this has been corrected by defining color::color() condtructor. Rest is all fine and code is getting compiled without any errors.

No image was getting created as value of m and n are not set. Currently assigned values to them in the default constructor. Also modified the color constructor that takes 3 arguments. File is now getting created.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 150;

class color
{
protected:
int a[SIZE][SIZE*3];
int m;
int n;

public:
    color();
    color(unsigned char, unsigned char, unsigned char);
    unsigned char r;
    unsigned char g;
    unsigned char b;
    static const int Width = 240;
    static const int height = 320;
    int getM()const;
    void setM(int m);
    int getN()const;
    void setN(int n);
    void writeppm(string);
    void createpicture();

};
int color::getM()const{
return m;
}
void color::setM(int M){
this->m=m;
}
int color::getN()const{
return n;
}
void color::setN(int n){
this->n=n;
}
color::color(){
m = 100;
n = 100;
r = 0;
g = 0;
b = 0;
}
color::color(unsigned char a, unsigned char b, unsigned char c){
r = a;
g = b;
b = c;
}
void color::writeppm(string filename){
    ofstream File(filename.c_str());
    int amount = 255;
    File << "P3" << endl;
    File << "Created by " << endl;
    File << Width << " " << height << endl;
    File << amount << endl;
    File << " " << endl;
    for (int i = 0; i < m; i++)
    {
    for (int j = 0; j < n*3; j++)
    File << a[i][j]<< endl;
    File << endl;
    }
    File << endl;

}
void color::createpicture(){
    color grey(127,127,127);

    for(int i = 0; i < m; i++)
    {
    for (int j = 0; j < n; j++)
    {
    a[i][j*3]=grey.r;
    a[i][j*3+1]=grey.g;
    a[i][j*3+2]=grey.b;
    }
    }
}

int main()
{
    color X;
    X.createpicture();
    X.writeppm("image1.ppm");
    cout << "Image has been created!!" << endl;
    return 0;
}