Одно замечание о 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;
}
РЕДАКТИРОВАТЬ : Похоже, что кто-то еще проходит такой же курс как ОП.Приведенный здесь пример проясняет, что Image должен иметь какой-то метод доступа к массиву, который может объяснить, чего достиг (*this)(x,y)->Red = (255 - (*this)(x,y)->Red)
.
EDIT 2 : Вотисточник для исходного класса BMP.