tags:

views:

120

answers:

2

I'm having a problem with the this pointer inside of a custom class. My code looks as follows.

class Foo(){
   public:  void bar();  bool baz();
};

bool Foo::baz(){
   return true;
}
void Foo::bar(){
   bool is_baz = (*this).baz();
}

As I said above, I believe the error I'm getting (LNK2019) is coming from the this. I think it is looking for a function in a different file, which it does not find. Is there some way I can make this code work, or do I have to use some sort of work-around? If so, what should I do to work around this problem. Thank you.

+6  A: 
class Foo(){

Change this to

class Foo{

Also, this shouldn’t compile. How did you manage to get a link error?

After making this change, the linker says undefined reference to 'main', which just means you don't have a main function.

jleedev
Yes, this strongly suggests that the code snippet is not the actual code but a garbled rewritten version, which will make it very difficult to help.
Daniel Earwicker
A: 

Although it is not an error, the line

bool is_baz = (*this).baz();

does not need the (*this) part. It can be written simply as

bool is_baz = baz();

But, what's the point of computing is_baz if it is neither used nor returned?

ArunSaha