C2011错误C 试图进行多态性
c2011 error c++ when trying to do 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);
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
您在两个不同的文件中声明您的类规模,在算法标题文件和.cpp文件中。实际上,如果您在Zoom函数中创建新图像,我不知道为什么如何使用继承。
您的标题,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;
}
无论如何,请考虑使用智能指针代替原始指针,并查看我所做的不同修改。
相关文章:
- 运行时多态性 - 箭头运算符访问了错误的成员?
- 如何避免指针超出范围(多态性)的C++分段错误
- 运行时与编译时多态性:更好的可读性与编译时错误检查,更重要的是
- C2011错误C 试图进行多态性
- 收到未定义的引用错误 c++ 继承/多态性
- 这种多态性隐喻是错误的吗?
- C++ 多态性错误
- 关于多态性和常量的错误
- 多态性>错误:'no known conversion from derived class to base class'
- C++多态性中的访问冲突(虚拟指针函数指向错误的位置?
- 涉及多态性的极其奇怪的错误
- C++ 代码多态性的错误结果
- 尝试使用多态性C++时发生编译错误
- 为什么为模板实例化声明运行时多态性会导致链接器错误
- c++代码中的动态多态性错误
- CRTP和动态多态性编译错误
- 多态性:代码中的复杂性错误.需要一个解释
- C++中的指针、多态性和分段错误
- 多态性错误C++
- 多态性 c++ 错误输出