开发者

Errors with 'this' pointer

开发者 https://www.devze.com 2023-01-18 10:44 出处:网络
I\'m having a problem with the this pointer inside of a custom class.My code looks as follows. class Foo{

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(){
   re开发者_运维问答turn 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.


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号