Thread: compile error cannot convert 'this' pointer

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    compile error cannot convert 'this' pointer

    Hello everyone,


    Which C++ rule do I break in the following code? Why there is compile error?

    Code:
    class Foo{
    public:
    	Foo()
    	{
    	}
    
    	int foo1()
    	{
    		return 100;
    	}
    
    	const Foo getCurrent1()
    	{
    		return *this;
    	}
    
    	const Foo& getCurrent2()
    	{
    		return *this;
    	}
    };
    
    int main()
    {
    	Foo f;
    	f.getCurrent1().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &'
    	f.getCurrent2().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &'
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I got a slightly more helpful warning message from gcc:

    error: no matching function for call to `Foo::foo1() const'

    foo1 is not labeled as a const function, therefore it cannot take a const "argument".

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks tabstop,


    Your fix works, cool!

    I am confused about what the compile outputs,

    cannot convert 'this' pointer from 'const Foo' to 'Foo &'

    I think it should change "this" to "*this", right? This pointer is a pointer to class (Foo*) and not class type itself (Foo)?

    Should be the following? Any comments?

    cannot convert '*this' from 'const Foo' to 'Foo &'

    Quote Originally Posted by tabstop View Post
    I got a slightly more helpful warning message from gcc:

    error: no matching function for call to `Foo::foo1() const'

    foo1 is not labeled as a const function, therefore it cannot take a const "argument".

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you think of member functions as normal functions that take the this reference (this is a pointer, but we'll treat it as a reference to match the compiler's message) as the first argument, foo1() becomes
    Code:
    int Foo::foo1(Foo *this);
    and the call becomes (this is invalid C++, you can't bind a temporary to a non-const reference)
    Code:
    Foo::foo1( Foo::getCurrent1(foo) )
    So you're passing a const object to a function that takes a non-const reference. Thus the failure to convert const Foo to Foo&.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    getCurrent1 and getCurrent2 both return const objects (temporary or otherwise). Therefore you can't call non-const method on them.

    As for the exact error message you get, yes maybe what you suggest is slightly more sensible, but I don't think it really matters.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    > you can't bind a temporary to a non-const reference

    I understand and agree with this rule.

    But after reading your reply a couple of times, I am confused. How do you get conclusion from,

    Code:
    int Foo::foo1(Foo *this);
    to

    Code:
    Foo::foo1( Foo::getCurrent1(foo) )
    What is foo? I read the code agian in my original question, there is no code or variable called foo?


    regards,
    George

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ah, sorry, you called that variable just f.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    Ah, sorry, you called that variable just f.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. One error left! Plz help, me=noob
    By rrum in forum C++ Programming
    Replies: 7
    Last Post: 12-01-2005, 01:09 AM
  5. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM