C++语法混淆——声明unsigned char数组

标签 c++ arrays syntax syntax-error

我有一个函数接受两个图像输入(im​​age 和 image2),逐个像素地混合颜色值。 Factor 表示图像颜色值的百分比,因此剩余值(1-factor)来自 image2。我有由无符号字符表示的像素缓冲区,我很难弄清楚我需要什么语法来访问和设置我的值。我已经尝试了很多事情,但现在它给了我以下错误:

filters.C:91:41: error: scalar object ‘p’ requires one element in initializer
 unsigned char *p = {red, green, blue};

函数:

void Filter::Execute()
{
    if (input->GetWidth() == input2->GetWidth() && input->GetHeight() == input2->GetHeight())
    {
        int width = input->GetWidth();
        int height = input->GetHeight();

        output.ResetSize(width, height);

        for (int w = 0; w < width; w++)
        {
            for (int h = 0; h < height; h++)
            {
                unsigned char red = input->GetPixel(w, h)[0]*factor+input2->GetPixel(w, h)[0]*(1-factor);
                unsigned char green = input->GetPixel(w, h)[1]*factor+input2->GetPixel(w, h)[1]*(1-factor);
                unsigned char blue = input->GetPixel(w, h)[2]*factor+input2->GetPixel(w, h)[2]*(1-factor);
                unsigned char *p = {red, green, blue};
                output.SetPixel(w, h, p);
            }
        }
    }
}

这就是我设置图像类的方式:

#include <image.h>
#include <stdlib.h>
Image::Image()
{
    width = 0;
    height = 0;
    buffer = NULL;
}

Image::~Image()
{
    if (buffer != NULL)
    {
        delete[] buffer;
    }
}

void Image::ResetSize(int w, int h)
{
    width = w;
    height = h;

    if (buffer != NULL)
    {
        delete[] buffer;
    }
    else
    {
        buffer = new unsigned char[3*width*height];

    }
}

unsigned char * Image::GetPixel(int w, int h)
{
    int index = h*width + w;
    return buffer+3*index;
}

void Image::SetPixel(int w, int h, unsigned char *p)
{
    int index = h*width + w;
    buffer[3*index+0] = p[0];
    buffer[3*index+1] = p[1];
    buffer[3*index+2] = p[2];
}

我忽略了什么?

最佳答案

unsigned char * 不是一个数组,它是一个指针。你要声明它

unsigned char p[] = {red, green, blue};

关于C++语法混淆——声明unsigned char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133413/

相关文章:

c++ - 在 FreeBSD 下用 c/c++ 复制大文件会卡住系统

javascript - 查找动态数组的对象元素 Javascript 中的公共(public)元素

syntax - 如何使用markdown语法编写包含右括号的链接?

c++ - 函数原型(prototype)中的 "..."

syntax - BNF 中括号和大括号的用法?

C++ 随机代理移动是相同的

c++ - 无法在不破坏堆叠顺序的情况下使 QQuickWidget 背景透明

c++ - 如何使用 QAxObject 捕获/抛出错误

javascript - 获取对象 ES6 中数组的总数/长度

javascript - 用于访问数组中对象的嵌套值的字符串