1

嗨,我想了解如何使用“this”指针。现在我编写了一个示例程序,它使用了一个类 Image,它是 BMP 类的子类。现在函数 TellWidth 和 TellHeight 在 BMP 类中声明。现在编译器给了我一个错误,说 Image 中不存在 TellWidth 函数。但是由于 Image 是 BMP 的子类,它不应该继承 BMP 中的功能。我该如何解决这个问题

void Image :: invertcolors()
{
    int x;
    int y;

    int width  =(*this).TellWidth();
    int height = (*this)->TellHeight();

    for(x=0,x<=height-1;x++){
        for(y=0,y<=width-1;y++){
            (*this)(x,y)->Red = (255 - (*this)(x,y)->Red);
            (*this)(x,y)->Blue = (255 - (*this)(x,y)->Blue);
            (*this)(x,y)->Green = (255 - (*this)(x,y)->Green);

        }
    }
    delete width;
    delete height;
}

图片

class Image : public BMP  
{
public:

    void invertcolors();

    void flipleft();
    void adjustbrightness(int r, int g, int b) ;

};

这个类太大了,不能在这里发,这里是一个相关的摘录

class BMP {
private:
   int Width;
   int Height;
public:
   int TellBitDepth(void) const;
   int TellWidth(void) const;
   int TellHeight(void) const;
};
4

3 回答 3

3

TellWidth()最有可能private在类中声明为(或没有访问器修饰符)BMP。它必须是protectedorpublicImage才能访问它virtual,如果您希望能够在Image类中覆盖它,它也必须是 。

正确的this用法是这样的:

int width = this->TellWidth();
int height = this->TellHeight();

阅读this有关this.

于 2011-01-28T02:21:45.407 回答
1

关于this: 你很少需要明确提及它。通常的例外是当您需要将其传递给非成员函数时(这里似乎不是这种情况。)

当你在一个类成员函数中时,this->field可以简单地访问field,并且this->function(x)可以调用function(x)

以下是对您的代码的一些评论。我希望他们会有所帮助。

void Image :: invertcolors()
{
    // Don't define these here; that's old C-style code. Declare them where
    // they're needed (in the loop: for (int x=0...)
    int x;
    int y;

    // Change the lines below to
    // int width  = TellWidth();
    // int height = TellHeight();
    //    (*this).TellWidth() should work, but is redundant;
    //    (*this)->TellHeight() should probably *not* work, as once you've
    //    dereferenced *this, you're dealing with an object instance, not a
    //    pointer. (There are ways to make (*this)->that() do something useful,
    //    but you're probably not trying something like that.)
    int width  =(*this).TellWidth();
    int height = (*this)->TellHeight();

    for(x=0,x<=height-1;x++){
        for(y=0,y<=width-1;y++){
            // After locating the BMP class through google (see Edit 2),
            // I've confirmed that (*this)(x,y) is invoking a (int,int) operator
            // on the BMP class. It wasn't obvious that this operator 
            // was defined; it would have been helpful if you'd posted
            // that part of the header file.
            (*this)(x,y)->Red = (255 - (*this)(x,y)->Red);
            (*this)(x,y)->Blue = (255 - (*this)(x,y)->Blue);
            (*this)(x,y)->Green = (255 - (*this)(x,y)->Green);

        }
    }
    // These are int values. They can't be deleted, nor do they need to be.
    // I'm sure the compiler has told you the same thing, though perhaps not
    // in the same way.
    delete width;
    delete height;
}

编辑:看起来还有其他人与 OP上相同的课程。那里提供的示例更清楚地表明 Image 应该具有某种数组访问器,这可以解释(*this)(x,y)->Red = (255 - (*this)(x,y)->Red)旨在实现的目标。

编辑 2这是原始 BMP 类的来源。

于 2011-01-28T03:10:15.943 回答
0

类 Image 定义为

class Image : public BMP  
{
public:

    void invertcolors();

    void flipleft();
    void adjustbrightness(int r, int g, int b) ;

};
于 2011-01-28T02:38:07.483 回答