假设图像是我的目标文件“a”,我创建了另一个名为 b 的目标文件来旋转图像。我正在将 a 的 (j,i) 赋给 b 的 (i,j),但我的代码无法正常工作,因为我没有正确使用称为运算符的函数,我有“//”我尝试过的东西,但我一直报错,怎么办? rgbapixels 是一个像素类,就像 png 是一个图像类一样;
在 PNG.h 中,我们现在将函数定义为;
#include <iostream>
#include "rgbapixel.h"
class PNG
{
public:
RGBAPixel * operator()(size_t x, size_t y);
/**
* Const pixel access operator. Const version of the previous
* operator(). Does not allow the image to be changed via the
* pointer.
* @param x X-coordinate for the pixel pointer to be grabbed from.
* @param y Y-cooridnate for the pixel pointer to be grabbed from.
* @return A pointer to the pixel at the given coordinates (can't
* change the pixel through this pointer).
*/
size_t width() const; // returns width
size_t width() const;
private:
// storage
size_t _width;
size_t _height;
RGBAPixel * _pixels;
};
在png.cpp 中为我们实现了这些功能。所以,在 main.cpp 中,我有使用它们的代码;
#include <algorithm>
#include <iostream>
#include "rgbapixel.h"
#include "png.h"
using namespace std;
int main()
{
cout << "me..........." << endl;
PNG a("I have put the image in "a" here");
PNG b;
for(size_t i = 0; i < a.width(); i++)
{
for(size_t j = 0; j <a.height(); j++)
{
// *b(i, j) = *a(j, i); erata
// b(i,j) = RGBAPixel * operator()(size_t x, size_t y);
// b(i, j) = operator()(i, j);
//b(i,j) = *operator(i,j);
//b(j,i) = a*operator(i,j);
//b(j,i) = a.operator(i,j);
//b(j, i) = a.*operator(i,j);
}
}
return 0;
}
我在使用该函数时遇到错误,我不知道哪里出了问题。所以,我不知道我的算法是否会使图像旋转
一些错误;
[jonathan2@linux-a1 lab_intro]$ make
clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp
main.cpp:24:29: error: expected ')'
b(i,j) = *operator(i,j);
^
main.cpp:24:28: note: to match this '('
b(i,j) = *operator(i,j);
^
main.cpp:24:20: error: use of undeclared 'operator()'
b(i,j) = *operator(i,j);
^
2 errors generated.
make: *** [main.o] Error 1
谢谢
最佳答案
()
运算符在具有此运算符的类的实例作为函数调用被调用时使用。
因此,对于您的 PNG
类,以及它的两个实例 a
和 b
,它们的 ()
运算符将被简单地调用为:
a(i, j)
或
b(i, j)
因为您的 ()
运算符返回一个 RGBAPixel *
,所以这种伪函数调用的最终结果是一个指针,我认为应该取消引用。
您的示例中第一行注释掉的代码:
// *b(i, j) = *a(j, i);
在我看来,这就像您的 ()
运算符的正确用法。我没有看到任何编译问题。
关于c++ - 你如何旋转给定的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597274/