auto_ptr assignment compile error

This is a discussion on auto_ptr assignment compile error within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I posted the code and related compile error below. My analysis below, 1. I think the reason of ...

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

    auto_ptr assignment compile error

    Hello everyone,


    I posted the code and related compile error below. My analysis below,

    1. I think the reason of compile error is, the copy constructor of Goo accepts const reference as input parameter, so the pf member of input parameter is also treated const?
    2. And since auto_ptr does not have a copy constructor which accepts const reference auto_ptr as input parameter, the compile error occurs.

    My analysis (1) and (2) are both correct?

    What makes me confused is it seems the compile error message does not reflect my analysis (1) and (2) above. Any ideas?

    Code:
    #include <memory>
    
    using namespace std;
    
    // Foo is Pimpl class for Goo, suppose.
    class Foo {
    
    };
    
    class Goo {
    
    public:
    
    	auto_ptr<Foo> pf;
    
    	Goo (const Goo& input)
    	{
    		this -> pf = input.pf;
    	}
    };

    1>d:\visual studio 2008\projects\test0330\test0330\main.cpp(18) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::auto_ptr<_Ty>' (or there is no acceptable conversion)
    1> with
    1> [
    1> _Ty=Foo
    1> ]
    1> d:\program files\microsoft visual studio 9.0\vc\include\memory(689): could be 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>:perator =<Foo>(std::auto_ptr<_Ty> &) throw()'
    1> with
    1> [
    1> _Ty=Foo
    1> ]
    1> d:\program files\microsoft visual studio 9.0\vc\include\memory(701): or 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>:perator =(std::auto_ptr<_Ty> &) throw()'
    1> with
    1> [
    1> _Ty=Foo
    1> ]
    1> d:\program files\microsoft visual studio 9.0\vc\include\memory(707): or 'std::auto_ptr<_Ty> &std::auto_ptr<_Ty>:perator =(std::auto_ptr_ref<_Ty>) throw()'
    1> with
    1> [
    1> _Ty=Foo
    1> ]
    1> while trying to match the argument list '(std::auto_ptr<_Ty>, const std::auto_ptr<_Ty>)'
    1> with
    1> [
    1> _Ty=Foo
    1> ]


    thanks in advance,
    George

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It looks to me that the compiler tries to tell you that std::auto_ptr doesn't have an assignement operator that takes a const reference to an auto_ptr as rhs ( and indeed there isn't any ).
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Your analysis is wrong in one way. You say copy constructor but what you should be saying is copy assignment operator. Switch that and that is exactly what the compiler is saying.

    No operator= was found that takes a const auto_ptr (and there are no acceptable conversions).

    If you initialized the auto_ptr in the initializer list (which you normally should) then you would get a similar error about the copy constructor.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    auto_ptr's operator=() function has to set the rhs parameter's contained pointer to NULL, so it obviously can't take a const auto_ptr parameter because then it wouldn't be allowed to change anything.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    So the root cause is from statement,

    this -> pf = input.pf;

    where since input is const, pf is const, but assignment operator of auto_ptr only accepts non-const?


    regards,
    George

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    this->pf is not const, but it has no assignment operator that accepts a const reference, which input.pf is (due to that it's being received as a const reference). I'm not sure if it's this that you were hinting at.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> So the root cause is from statement
    Yes, the cause is from that statement, but you would get a similar error if you initialized the auto_ptr in the initializer list.

    I would say the root cause is the attempt to transfer ownership of the auto_ptr in the copy constructor.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia and Daved,


    Question answered.

    Quote Originally Posted by Daved View Post
    >> So the root cause is from statement
    Yes, the cause is from that statement, but you would get a similar error if you initialized the auto_ptr in the initializer list.

    I would say the root cause is the attempt to transfer ownership of the auto_ptr in the copy constructor.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21