Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c2011错误c++;在尝试执行多态性时_C++_Polymorphism - Fatal编程技术网

C++ c2011错误c++;在尝试执行多态性时

C++ c2011错误c++;在尝试执行多态性时,c++,polymorphism,C++,Polymorphism,我试图通过我的图像处理程序实现多态性。我不断地遇到这个错误,我想这是因为我在头文件和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)

我试图通过我的图像处理程序实现多态性。我不断地遇到这个错误,我想这是因为我在头文件和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)];
                }
            }
        }    
};

您在两个不同的文件中声明您的类比例,即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;
}  
然后,在您想要使用该类的地方,例如您的main:

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

在任何情况下,考虑使用智能指针代替原始指针,并查看我所做的不同修改。 您在两个不同的文件中声明您的类比例,即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;
}  
然后,在您想要使用该类的地方,例如您的main:

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

在任何情况下,考虑使用智能指针代替原始指针,并查看我所做的不同修改。 不要在cpp文件中声明类。您只能在标题中执行此操作。编译器是正确的,您不应该重新定义类。另外,cpp文件中类上方的所有代码是什么?这毫无意义。当你编写代码时,先从一些小而简单的、工作完美的东西开始,然后一次增加一点复杂性。这样的话,当代码是当前大小的五十分之一时,您就可以发现并处理这个bug了。我们不是在几个小时前从不同的用户那里看到了几乎相同的代码吗。。虽然这是不同的错误。但不要在cpp文件中声明类。您只能在标题中执行此操作。编译器是正确的,您不应该重新定义类。另外,cpp文件中类上方的所有代码是什么?这毫无意义。当你编写代码时,先从一些小而简单的、工作完美的东西开始,然后一次增加一点复杂性。这样的话,当代码是当前大小的五十分之一时,您就可以发现并处理这个bug了。我们不是在几个小时前从不同的用户那里看到了几乎相同的代码吗。。虽然那是不同的错误。