c2011错误c ++尝试做多态时

问题描述 投票:0回答:1

我正试图用我的图像处理程序实现多态性。我一直收到这个错误,我认为这是因为我在头文件和cpp文件中两次定义scale

Error   C2011   'scale': 'class' type redefinition  

我不知道该怎么办谢谢你的帮助。

.cpp文件

   Image *begin= new Image(750, 750);
   begin->read("picture1.jpg");
   Image *enlarged= new Image(1500, 1500);

    scale *pass= new scale(*imgScale);
    pass->manipulator(1500, 1500);

    pass->load("manipulated.jpg");

    class scale : public Image
    {
    public:    
        scale(Image const &firstimg)
        {
             w = firstimg.w;
             h = firstimg.h;
            pixels = firstimg.pixels;
        }

        void manipulator(int w2, int h2)
        {
            Image *temp = new Image(w2, h2);
            float x_ratio = (float )w / w2;
            float y_ratio = (float )h / h2;
            float px, py;
            for (int i = 0; i < h2; i++)
            {
                for (int j = 0; j < w2; j++)
                {
                    px = floor(j*x_ratio);
                    py = floor(i*y_ratio);
                    temp->pixels[(i*w2) + j] = this->pixels[(int)((py*w) + px)];
                }
            }
        }    
};

头文件

#pragma once    
#ifndef manipulator_H
#define manipulator_H

class scale : public Image
{
    public:
        scale(Image const &firstimg);

        void manipulator(int w2, int h2);
};
#endif
c++ polymorphism
1个回答
2
投票

您正在声明您在algo头文件和.cpp文件中的两个不同文件中扩展。实际上,如果你在缩放功能中创建一个新的图像,我不知道为什么使用继承。

你的标题,scale.h应该是这样的:

#pragma once    
#ifndef ALGORITHMS_H
#define ALGORITHMS_H

class Image;    
class Scale {
    public:
        explicit Scale(Image const &beginImg);
        void zoom(int w2, int h2);

    private:
    // Here all your private variables
    int w;
    int h;
    ¿? pixels;
};
#endif

你的cpp文件,scale.cpp:

#include "scale.h"
#include "image.h"
Scale::Scale(Image const &beginImg) : 
        w(beginImg.w),
        h(beginImg.h),
        pixels(beginImg.pixels) {}

void Scale::zoom(int w2, int h2){
       Image *temp = new Image(w2, h2);
       double x_ratio = (double)w / w2;
       double y_ratio = (double)h / h2;
       double px, py;
       // rest of your code;
}  

然后,在你想要使用这个类的地方,例如你的主要:

int main() {
    Image *imgScale = new Image(750, 750);
    imgScale->readPPM("Images/Zoom/zIMG_1.ppm");

    Scale *test = new Scale(*imgScale);
    test->zoom(1500, 1500);

    test->writePPM("Scale_x2.ppm");

    delete imgScale;
    delete test;
    return 0;
}

在任何情况下,请考虑使用智能指针而不是原始指针,并查看我所做的不同修改。

© www.soinside.com 2019 - 2024. All rights reserved.