在没有 setter 的情况下修改基类中的私有数据,c++ visual studio 2013

Modify private data in a base class without a setter, c++ visual studio 2013

本文关键字:数据 c++ visual studio 2013 基类 setter 情况下 修改      更新时间:2023-10-16

我正在为我的计算机科学课做一个项目,在那里我们创建了一个源图像的照片马赛克。我们得到了一个基本图像类。与标准构造函数一起,此类包含用于返回私有像素数组数据和图像高度和宽度的 getter 函数。我们被告知为所有填充图像创建一个派生类,这些图像需要存储在链表中。我已经完成了大部分程序,但我不知道如何用填充图像中的像素数据替换源图像的块。

    #include "stdafx.h"  //needed for the Windows display
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <iostream>
    using namespace std;
    #include "globals.h"  //some global variables are included here
    #include "pixel.h"  //includes the pixel class for storing individual pixels
    #include "image.h"  //includes the image class we will be using.
    #include "fillerImage.h" //includes the fillerImage
    #include "fillNode.h"
    #include <sstream>

image* mosaic = displayed;  //displayed is the current image in the gui, mosaic is the copy that will be the photomosaic
pixel** mospix = mosaic->getPixels();  
pixel** headpix = head->data->pix;   //I created a public member pixel** in the filler image class with the hope that I could use it to access the private pixel data of the base image class.
int mosaicWidth = displayed->getWidth() / blockWidth * FILLER_IMAGE_WIDTH;
int mosaicHeight = displayed->getHeight() / blockHeight * FILLER_IMAGE_HEIGHT;
//mosaic->createNewImage(mosaicWidth, mosaicHeight);
for (int i = 0; i < mosaicHeight; i = i + blockHeight)
{
    for (int j = 0; j < mosaicWidth; j = j + blockWidth)
    {
        for (int blockrow = 0; blockrow < blockHeight; blockrow++)
        {
            for (int blockcol = 0; blockcol < blockWidth; blockcol++)
            {
                mospix = headpix;
            }
        }
    }
    displayed = mosaic;
}
displayed = mosaic;

我一直遇到尝试写入受保护数据的问题;如何修改马赛克的像素数据?我不允许编辑基本图像类,只能编辑填充图像类。如果我的帖子格式不正确,我深表歉意,这是我第一次寻求帮助。提前感谢!

编辑:这是图像类标题。我不允许以任何方式修改它,它不包含像素数据的设置器。

    #include "globals.h"
    #include "pixel.h"
    using namespace std;
    class image { 
public:
    image();            //the image constructor (initializes everything)
    image(string filename);  //a image constructor that directly loads an image from disk
    ~image();           //the image destructor  (deletes the dynamically created pixel array)
    void createNewImage(int width, int height); //this function deletes any current image data and creates a new blank image
                                                //with the specified width/height and allocates the needed number of pixels
                                                //dynamically.
    bool loadImage(string filename);        //load an image from the specified file path.  Return true if it works, false if it is not a valid image.
                                            //Note that we only accept images of the RGB 8bit colorspace!
    void saveImage(string filename);       //Save an image to the specified path
    pixel** getPixels();                    //return the 2-dimensional pixels array
    int getWidth();                     //return the width of the image
    int getHeight();                    //return the height of the image
    void viewImage(CImage* myImage);  //This function is called by the windows GUI.  It returns the image in format the GUI understands.

private:
    void pixelsToCImage(CImage* myImage);  //this function is called internally by the image class.
                                            //it converts our pixel object array to a standard BGR uchar array with word spacing.
                                            //(Don't worry about what this does)
    pixel** pixels;             // pixel data array for image 
    int width, height;      // stores the image dimensions 


    };
    #endif

访问像素的唯一方法是通过getPixel()

  • 你可以从中明确地阅读。
  • 由于此成员函数不返回const指针,并且在没有文档的情况下,我假设您也可以在其上编写。

使用以下语句,您可以访问 mosaic 的像素数组。

    pixel** mospix = mosaic->getPixels();  

从那一刻起,您可以使用mospix[i][j]直接读取/写入任何像素,只要0 <= i && i < mosaic->getHeight() && 0<=j && j<mosaic->getWidth()

但是,您的代码中存在一个严重的问题。在嵌套循环中,您的内部语句是:

            mospix = headpix;

这不会像您期望的那样复制当前像素。 它将您正确初始化的指针替换为指针的副本 headpix 。 从那时起,如果您访问mospix数组元素,实际上您会让里面的图片保持不变mosaic但您将访问head->data像素!

我不知道你想如何创建马赛克,但我认为你的循环方式不匹配,可以想象你想说悲惨的话:

for (int i = 0; i < mosaic->getHeight(); i = i + blockHeight)
{
    for (int blockrow = 0; blockrow < blockHeight; blockrow++)
    {
        for (int j = 0; j < mosaic->getWidth(); j = j + blockWidth)
        {
            for (int blockcol = 0; blockcol < blockWidth; blockcol++)
            {
                mospix[i+blockrow][j+blockcol] = headpix[blockrow][blockcol];
            }
        }
    }
}
displayed = mosaic;  // Not sure it's needed: both are pointers pointing to the same object