【问题标题】:C++ Problems with Class Arrays [closed]类数组的 C++ 问题
【发布时间】:2014-12-05 20:18:27
【问题描述】:

//#程序是读入ppm文件并在命令提示符中显示# //#另外它必须能够拍两张照片并叠加在另一张上#

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

// 将高度和宽度设置为零

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

//复制Image类

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;
    }

//加载用户在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 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 颜色.[i].g = 绿色; // 错误:需要一个标识符 颜色[i].b = 蓝色; // *Error:expected an identifier: expression must have a con // 如果你能帮助我,请提前谢谢你

【问题讨论】:

  • 那么问题是什么/你遇到了什么问题?
  • 当您使用调试器并单独执行每一行时,您发现了什么?
  • 我在 saveimage 函数中遇到错误,但我不知道为什么

标签: c++ arrays function class rgb


【解决方案1】:

您正在为数组使用颜色,而不是您制作的“像素”数组。更改这些行:

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*

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多