0

Sorry not sure if this has been asked before, I really dont know what to look up either. I'm new to C++ from Java. When we want to call a function on an object in Java, we say picture.rotateRight();

Then, in rotateRight(), we'd have something like int height=this.getHeight();. However, how do we do this in C++? I have a method named invertcolors(); and then I have something like:

Image* myImage = new Image();
bool b = myImage->ReadFromFile("in_01.bmp");
myImage->invertcolors();

void invertcolors(){
    int width=TellWidth();
    int height=TellHeight();
    ...
}

How do I access myImage from the method definition without actually saying myImage (since that name can later be changed).

Also, the function parameters are non-negotiable.

Snowman
  • 31,411
  • 46
  • 180
  • 303
  • 2
    Do you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/)? If not, you really should get one. It's all but impossible to learn to write correct C++ without one. – James McNellis Jan 28 '11 at 01:23
  • 3
    If you're new to C++, forget that you learned Java, [pick up a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read through it, as James McNellis have already mentioned. – In silico Jan 28 '11 at 01:24
  • @James McNellis: Well, it's still very hard, even with a good introductory book. – jason Jan 28 '11 at 01:24

7 Answers7

5

First of all, your invertcolors() function definition is a non-member function. Although you've declared it inside the Image class, you haven't linked the implementation to the class in any way so the compiler thinks its a non-member function. To make it a member of Image, you need to use Image::invertcolors like this:

void Image::invertcolors(){
    int width=TellWidth();
    int height=TellHeight();
    ...
}

You do get this in C++, but it's a pointer so you have to use this->getHeight() in C++. However, note that it is redundant in this case. As a beginner you'll probably find the only real use in a method having the same argument name as an attribute. In this case, you'll need to use this->height = height for example. However, note that C++ has a nice syntax addition here. This code does the same as a simple setter:

void Image::setHeight(int height): height(height) {}

Note that neither in Java nor C++ is this an operator. ., -> and + are examples of operators.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • I tried this but I ge these errors: `error: invalid use of ‘this’ in non-member function` – Snowman Jan 28 '11 at 01:25
  • @mohabitar: You need to make `invertcolors()` a member of the `Image` class (both its declaration *and* its definition). Again, please [pick up a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), which will cover how to properly work with classes. – In silico Jan 28 '11 at 01:25
  • @mohabitar: The compiler thinks `invertcolors()` is a non-member function, so it's not in the `Image` class. Can you edit your question to include the `Image` class declaration so we can see what's up with it? – In silico Jan 28 '11 at 01:27
  • @mohabitar Note the `Image::` as in my example. You might be missing this. – moinudin Jan 28 '11 at 01:28
  • 2
    @mohabitar: For your member function definition you would write `void Image::invertcolors()` in your first code snippet. You must specify which class the function declaration belongs to, otherwise it's a non-member function. Again, I deplore you to pick up a good C++ book and read through it. [Here's a link to a list of books recommended by the Stack Overflow community](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You're struggling with rather basic C++; learn it right or you will struggle with the rest of C++. – In silico Jan 28 '11 at 01:29
  • @marcog: I don't think you can use a *ctor-initializer-list* except on constructors. What compiler accepts this? – Ben Voigt Jan 28 '11 at 01:31
  • Ok I think that did the trick, but I gotta do more testing. Do we always have to use ::, even if the functions are already declared under the Image class? – Snowman Jan 28 '11 at 01:32
  • @mohabitar: Yes, if you want to declare and define the member functions separately, you have to tell the compiler that they both belong to the `Image` class. You do that by putting the method declaration within the `Image` class itself, and putting `Image::` before the method name in the definition. I'm going to say this again: please pick up [a good C++ book](http://stackoverflow.com/questions/388242) and read it. It'll tell you how to deal with classes in C++ and much more. (By the way, do not buy any C++ books from Herbert Schildt. His C++ books are garbage.) – In silico Jan 28 '11 at 01:35
  • @mohabitar: If you put the function definition inside the class definition, like Java, you don't need to qualify it with `Image::`. If the declaration is outside the class body, you will need to qualify with the class name (and maybe also namespace, nested class, etc.) – Ben Voigt Jan 28 '11 at 01:36
  • Ok I'm still a bit confused. I have this image, and using the EasyBMP library, I can access its individual RGB values using `greenValue=Image(x,y)->Green;`. However, since I cant use the name Image, I tried `greenValue=this->(x,y)->Green;`, but that didnt work. What am I doing wrong? – Snowman Jan 28 '11 at 01:38
  • @mohabitar: Is this a class you wrote, or someone else's? If you want to call an operator on your own class, it gets pretty ugly. Best thing to do is create a reference, like: `Image& self = *this; self(x, y);`. Without the reference, you'll need something like `(*this)(x, y)`. Oh now I see, you've got a class that inherits the existing BMP class. The syntax I'm suggesting will work for invoking operators defined in the base class as well. – Ben Voigt Jan 28 '11 at 01:40
2

this is a keyword, not an operator, and it does exist in C++. It's a pointer, so you'll use it with ->, not ., when accessing members.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

Inside a member function, this->whatever is implicit, so you can just use whatever on its own, and the compiler will figure out that you mean this->whatever. There are a few cases (mostly in templates) that it can make sense to use this-> in C++ as well, but it's only rarely necessary (I'm aware of the times, but after writing C++ for a couple of decades, I can probably still count the times I've done it on my fingers).

If your code is not in a member function, then it has to explicitly refer to some particular object (much as in Java).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

"this" is also available in C++. It's a pointer to the object on which the function is being called.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
0

EDIT: Sorry, didn't see the part about the parameters. New Answer: If the function parameters are non-negotiable, and you can't be arsed to define invertColors as a member function for Image (by extending the Image class) then you'll need a globally defined variable.

Outside of your main function, declare Image* myimg;

then use myimg like normal inside your function.

ex:

Image* myImg;
int main()
{
    ..all your initialization, etc. 
    invertColors();
}

void invertColors()
{
     int width = myImg->width;
     ...
}
AndyG
  • 39,700
  • 8
  • 109
  • 143
0

Assuming that invertcolors(), TellWidth(), and TellHeight() are member functions of the Image class, it will work just as you've written it.

In C++, this is a const-pointer to the object on which the method was invoked (as opposed to Java, where this is a reference). So, rather than:

this.doSomething(); // in Java

you'd say:

this->doSomething(); // in C++.
Drew Hall
  • 28,429
  • 12
  • 61
  • 81
0

Reading the comments, are you missing how to define the method:

Header file: Image.h

class Image : public BMP {

public:

    void invertcolors();
};

Source file Image.cpp

void Image::invertcolors()
{
    int width=TellWidth();
    int height=TellHeight();
    // ...
}
Keith
  • 6,756
  • 19
  • 23