2014-12-05 91 views
-5

//#方案是以ppm文件的讀取和在命令提示# 顯示它//#此外,它必須能夠採取兩個圖片和疊加在彼此之上#C++問題與級陣列

#include "Image.h" 
#include "P3Loader.h" 
#include "P6Loader.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

//設置高度和寬度爲零

Image::Image() 
    { 
     width = height = 0; 
    } 

//複製圖像類

Image::Image(const Image& copy) 
{ 
    width = copy.width; 
    height = copy.height; 
    loader = copy.loader; 

    pixels = new Color[copy.height]; 
    for(int i = 0; i < 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]; 
     } 
    } 
} 

個//零的所有像素進行

Image::~Image() 
    { 
     delete[] pixels; 
    } 

//加載該用戶選擇在主

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 a vaild Format" << endl; 
     exit(1); 
    } 
} 

//保存在臨時文件圖像

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 the image data to temp 
    imageFile.read(temp,width*height*3); 

    pixels = new Color[width*height]; 
    for (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 an identifier: expression must have a constant val* 
     Color.[i].g = green; // *Error: expected an identifier* 
     Color[i].b = blue; // *Error:expected an identifier: expression must have a constant val* 
    } 

} 

//運算符=過載的文件

Image Image::operator=(const Image& other) 
    { 
     height = other.height; 
     width = other.width; 
     loader = other.loader; 
     pixels = other.pixels; 

     return *this; 
    } 

//功能爲在另一個之上

void superimpose(const Image& ontop, Color mask) 
    { 

    } 

// 強加一個圖象獲取文件高度

int getheight() 
{ 
    int height; 
    int width; 
    string magic_number; 

    ifstream picture; 
    picture.open(filename); 

    picture >> magic_number >> width >> height; 
    return height; // send it back w, h 
} 

// 獲取文件類型和寬度

int getwidth() 
{ 
    int width; 

    string magic_number; 
    ifstream picture; 
    picture.open(filename); 
    picture >> magic_number >> width; 
    return width; // send it back w, h 
} 

顏色[I] .R =紅色; // 錯誤:預計的標識符:表達式必須具有常數val Color。[i] .g = green; // 錯誤:預計標識符 Color [i] .b = blue; // *錯誤:預期標識符:expression必須有一個con //如果您可以幫助我,請提前致謝

+3

那麼究竟是什麼問題/你遇到了什麼問題? – marsh 2014-12-05 20:20:04

+0

當你使用* debugger *並單獨執行每一行時,你發現了什麼? – 2014-12-05 20:31:42

+0

我在saveimage函數中出現錯誤,但我不知道爲什麼 – 2014-12-05 20:55:41

回答

0

您正在使用Color作爲數組而不是使用「像素」的數組。更改這些行:

Color[i].r = red; // *Error: expected an identifier: expression must have a constant val* 
Color.[i].g = green; // *Error: expected an identifier* 
Color[i].b = blue; // *Error:expected an identifier: expression must have a constant val* 

到:

pixels[i].r = red; // *Error: expected an identifier: expression must have a constant val* 
pixels[i].g = green; // *Error: expected an identifier* 
pixels[i].b = blue; // *Error:expected an identifier: expression must have a constant val*