C++ Problems with Class Arrays -
//#program read in ppm file , display in command prompt# //#also must able take 2 pictures , superimpose 1 on other#
#include "image.h" #include "p3loader.h" #include "p6loader.h" #include <iostream> #include <fstream> #include <string>
// sets height , width zero
image::image() { width = height = 0; }
// copies image class
image::image(const image& copy) { width = copy.width; height = copy.height; loader = copy.loader; pixels = new color[copy.height]; for(int = 0; < copy.height; i++) { pixels = new color[copy.width]; } for(int h = 0; h < copy.height; h++) { for(int w = 0; w < copy.width; w++) { pixels[h*w] = copy.pixels[h*w]; } } }
// zeros pixels out
image::~image() { delete[] pixels; }
//loads file user selects in main
void image::loadimage(string filename) { ifstream picture; string magic_number; //enter filename picture.open(filename); if(picture.fail()) { cout << "error... file not opened" << endl; } picture >> magic_number; if(magic_number == "p3") { loader = new p3loader; } else if (magic_number == "p6") { loader = new p6loader; } else { cout << "not vaild format" << endl; exit(1); } }
// saves image in temp file
void image::saveimage(string filename) { ifstream imagefile; string magic_number; char *temp; imagefile.open(filename); imagefile >> magic_number >> width >> height; imagefile.get(); temp = new char[width*height*3]; // read in image data temp imagefile.read(temp,width*height*3); pixels = new color[width*height]; (int i=0; i<width*height; i++) { unsigned char red = temp[i*3]; unsigned char green = temp[i*3+1]; unsigned char blue = temp[i*3+2]; color[i].r = red; // *error: expected identifier: expression must have constant val* color.[i].g = green; // *error: expected identifier* color[i].b = blue; // *error:expected identifier: expression must have constant val* } }
//operator= overload
image image::operator=(const image& other) { height = other.height; width = other.width; loader = other.loader; pixels = other.pixels; return *this; }
// function impose 1 picture upon another
void superimpose(const image& ontop, color mask) { }
// gets files height
int getheight() { int height; int width; string magic_number; ifstream picture; picture.open(filename); picture >> magic_number >> width >> height; return height; // send w, h }
// gets file type , width
int getwidth() { int width; string magic_number; ifstream picture; picture.open(filename); picture >> magic_number >> width; return width; // send w, h }
color[i].r = red; // error: expected identifier: expression must have constant val color.[i].g = green; // error: expected identifier color[i].b = blue; // *error:expected identifier: expression must have con // thank in advance if can me
you using color array instead of array made "pixels." change these lines:
color[i].r = red; // *error: expected identifier: expression must have constant val* color.[i].g = green; // *error: expected identifier* color[i].b = blue; // *error:expected identifier: expression must have constant val*
to:
pixels[i].r = red; // *error: expected identifier: expression must have constant val* pixels[i].g = green; // *error: expected identifier* pixels[i].b = blue; // *error:expected identifier: expression must have constant val*
Comments
Post a Comment