compiler error I don't understand

Discussion in 'C++' started by Tobias Langner, Aug 1, 2003.

  1. compiling this line:

    SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    LongWrapper(1));

    with SmartPtr being:

    template <class T, template <class> class OwnershipPolicy>
    class SmartPtr : public OwnershipPolicy<T> {
    public:
    explicit SmartPtr(T* pointer);
    SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy> &);
    SmartPtr<T, OwnershipPolicy> &operator=(T*);
    ~SmartPtr();
    bool isNULL() { return pointer_==NULL; }
    T& operator*() const;
    T* operator->() const;


    private:
    void deleteObject();
    void copyPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    void copyPtr(T* otherPointer);
    SmartPtr();
    T* pointer_;

    };

    i get the following error:

    test.cpp:22: no matching function for call to `SmartPtr<LongWrapper,
    RefCount>::SmartPtr(SmartPtr<LongWrapper, RefCount>)' smartptr.h:105:
    candidates are: SmartPtr<T, OwnershipPolicy>::SmartPtr(SmartPtr<T,
    OwnershipPolicy>&) [with T = LongWrapper, OwnershipPolicy = RefCount]

    wich I don't understand.
     
    Tobias Langner, Aug 1, 2003
    #1
    1. Advertising

  2. "Tobias Langner" <> wrote in message
    news:bgef9i$1pt$05$-online.com...
    > compiling this line:
    >
    > SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    > LongWrapper(1));
    >
    > with SmartPtr being:
    >
    > template <class T, template <class> class OwnershipPolicy>
    > class SmartPtr : public OwnershipPolicy<T> {
    > public:
    > explicit SmartPtr(T* pointer);
    > SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy> &);
    > SmartPtr<T, OwnershipPolicy> &operator=(T*);
    > ~SmartPtr();
    > bool isNULL() { return pointer_==NULL; }
    > T& operator*() const;
    > T* operator->() const;
    >
    >
    > private:
    > void deleteObject();
    > void copyPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > void copyPtr(T* otherPointer);
    > SmartPtr();
    > T* pointer_;
    >
    > };
    >
    > i get the following error:
    >
    > test.cpp:22: no matching function for call to `SmartPtr<LongWrapper,
    > RefCount>::SmartPtr(SmartPtr<LongWrapper, RefCount>)' smartptr.h:105:
    > candidates are: SmartPtr<T, OwnershipPolicy>::SmartPtr(SmartPtr<T,
    > OwnershipPolicy>&) [with T = LongWrapper, OwnershipPolicy = RefCount]
    >
    > wich I don't understand.


    Compiles fine for me once I've added dummy definitions for LongWrapper and
    RefCount. Post minimal, compilable code please.

    john
     
    John Harrison, Aug 1, 2003
    #2
    1. Advertising

  3. "Tobias Langner" <> wrote in message
    news:bgef9i$1pt$05$-online.com...
    > compiling this line:
    >
    > SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    > LongWrapper(1));


    The above call creates a temporary SmartPtr object which is used to copy
    construct 'p1'.

    > with SmartPtr being:
    >
    > template <class T, template <class> class OwnershipPolicy>
    > class SmartPtr : public OwnershipPolicy<T> {
    > public:
    > explicit SmartPtr(T* pointer);
    > SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy> &);


    The above copy c'tor and operator= both take a reference to a SmartPtr
    object. But references cannot be bound to temporaries, which you are trying
    to do above. This is the reason, why the copy c'tor and the operator= should
    both take /const/ references, because temporaries /can/ be bound to those.

    > SmartPtr<T, OwnershipPolicy> &operator=(T*);
    > ~SmartPtr();
    > bool isNULL() { return pointer_==NULL; }
    > T& operator*() const;
    > T* operator->() const;
    >
    >
    > private:
    > void deleteObject();
    > void copyPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > void copyPtr(T* otherPointer);
    > SmartPtr();
    > T* pointer_;
    >
    > };
    >
    > i get the following error:
    >
    > test.cpp:22: no matching function for call to `SmartPtr<LongWrapper,
    > RefCount>::SmartPtr(SmartPtr<LongWrapper, RefCount>)' smartptr.h:105:
    > candidates are: SmartPtr<T, OwnershipPolicy>::SmartPtr(SmartPtr<T,
    > OwnershipPolicy>&) [with T = LongWrapper, OwnershipPolicy = RefCount]
    >
    > wich I don't understand.


    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Aug 1, 2003
    #3
  4. "John Harrison" <> wrote in message
    news:bgegh7$o6ne7$-berlin.de...
    >
    > "Tobias Langner" <> wrote in message
    > news:bgef9i$1pt$05$-online.com...
    > > compiling this line:
    > >
    > > SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    > > LongWrapper(1));
    > >
    > > with SmartPtr being:
    > >
    > > template <class T, template <class> class OwnershipPolicy>
    > > class SmartPtr : public OwnershipPolicy<T> {
    > > public:
    > > explicit SmartPtr(T* pointer);
    > > SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > > SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy>

    &);
    > > SmartPtr<T, OwnershipPolicy> &operator=(T*);
    > > ~SmartPtr();
    > > bool isNULL() { return pointer_==NULL; }
    > > T& operator*() const;
    > > T* operator->() const;
    > >
    > >
    > > private:
    > > void deleteObject();
    > > void copyPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > > void copyPtr(T* otherPointer);
    > > SmartPtr();
    > > T* pointer_;
    > >
    > > };
    > >
    > > i get the following error:
    > >
    > > test.cpp:22: no matching function for call to `SmartPtr<LongWrapper,
    > > RefCount>::SmartPtr(SmartPtr<LongWrapper, RefCount>)' smartptr.h:105:
    > > candidates are: SmartPtr<T, OwnershipPolicy>::SmartPtr(SmartPtr<T,
    > > OwnershipPolicy>&) [with T = LongWrapper, OwnershipPolicy = RefCount]
    > >
    > > wich I don't understand.

    >
    > Compiles fine for me once I've added dummy definitions for LongWrapper and
    > RefCount. Post minimal, compilable code please.



    Which compiler did you use? I did not compile it, since I was too laze
    to add the dummy definitions, but as far as I can tell, it should not
    compile. See my reply to the OP.

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Aug 1, 2003
    #4
  5. Jakob Bieling wrote:

    > "Tobias Langner" <> wrote in message
    > news:bgef9i$1pt$05$-online.com...
    >> compiling this line:
    >>
    >> SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    >> LongWrapper(1));

    >
    > The above call creates a temporary SmartPtr object which is used to
    > copy
    > construct 'p1'.
    >
    >> with SmartPtr being:
    >>
    >> template <class T, template <class> class OwnershipPolicy>
    >> class SmartPtr : public OwnershipPolicy<T> {
    >> public:
    >> explicit SmartPtr(T* pointer);
    >> SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    >> SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy>
    >> &);

    >
    > The above copy c'tor and operator= both take a reference to a SmartPtr
    > object. But references cannot be bound to temporaries, which you are
    > trying to do above. This is the reason, why the copy c'tor and the
    > operator= should both take /const/ references, because temporaries /can/
    > be bound to those.
    >

    ok, I do understand that, but I actually need them to be non-const, since I
    do want to implement a destructive-copy, so SmartPtr(SmartPtr<T,
    OwnershipPoliy& otherPointer) may change otherPointer. Any suggestions?
     
    Tobias Langner, Aug 1, 2003
    #5
  6. "Tobias Langner" <> wrote in message
    news:bgehj2$4lv$02$-online.com...
    > Jakob Bieling wrote:
    >
    > > "Tobias Langner" <> wrote in message
    > > news:bgef9i$1pt$05$-online.com...
    > >> compiling this line:
    > >>
    > >> SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    > >> LongWrapper(1));

    > >
    > > The above call creates a temporary SmartPtr object which is used to
    > > copy
    > > construct 'p1'.
    > >
    > >> with SmartPtr being:
    > >>
    > >> template <class T, template <class> class OwnershipPolicy>
    > >> class SmartPtr : public OwnershipPolicy<T> {
    > >> public:
    > >> explicit SmartPtr(T* pointer);
    > >> SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > >> SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy>
    > >> &);

    > >
    > > The above copy c'tor and operator= both take a reference to a

    SmartPtr
    > > object. But references cannot be bound to temporaries, which you are
    > > trying to do above. This is the reason, why the copy c'tor and the
    > > operator= should both take /const/ references, because temporaries /can/
    > > be bound to those.
    > >

    > ok, I do understand that, but I actually need them to be non-const, since

    I
    > do want to implement a destructive-copy, so SmartPtr(SmartPtr<T,
    > OwnershipPoliy& otherPointer) may change otherPointer. Any suggestions?


    Either do not use temporaries, or use a special function that implements
    destructive copy, which the user has to call. Personally, I would go for the
    special-method approach.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Aug 1, 2003
    #6
  7. Jakob Bieling wrote:

    > "Tobias Langner" <> wrote in message
    > news:bgehj2$4lv$02$-online.com...
    >> Jakob Bieling wrote:
    >>
    >> > "Tobias Langner" <> wrote in message
    >> > news:bgef9i$1pt$05$-online.com...
    >> >> compiling this line:
    >> >>
    >> >> SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper,
    >> >> RefCount>(new
    >> >> LongWrapper(1));
    >> >
    >> > The above call creates a temporary SmartPtr object which is used to
    >> > copy
    >> > construct 'p1'.
    >> >
    >> >> with SmartPtr being:
    >> >>
    >> >> template <class T, template <class> class OwnershipPolicy>
    >> >> class SmartPtr : public OwnershipPolicy<T> {
    >> >> public:
    >> >> explicit SmartPtr(T* pointer);
    >> >> SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    >> >> SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T, OwnershipPolicy>
    >> >> &);
    >> >
    >> > The above copy c'tor and operator= both take a reference to a

    > SmartPtr
    >> > object. But references cannot be bound to temporaries, which you are
    >> > trying to do above. This is the reason, why the copy c'tor and the
    >> > operator= should both take /const/ references, because temporaries
    >> > /can/ be bound to those.
    >> >

    >> ok, I do understand that, but I actually need them to be non-const, since

    > I
    >> do want to implement a destructive-copy, so SmartPtr(SmartPtr<T,
    >> OwnershipPoliy& otherPointer) may change otherPointer. Any suggestions?

    >
    > Either do not use temporaries, or use a special function that
    > implements
    > destructive copy, which the user has to call. Personally, I would go for
    > the special-method approach.
    >

    Actually I tried to implement different behaviour according to the
    template-parameter OwnershipPolicy. If you create your SmartPointer<T,
    RefCount>, you have a ref-counting one, if you create a SmartPointer<T,
    DestructiveCopy>, you get one with destructive copy, if you create
    SmartPointer<T, DeepCopy> you get one that does a deep copy. That would not
    be possible with a special method.
     
    Tobias Langner, Aug 1, 2003
    #7
  8. "John Harrison" <> wrote in message
    news:bgelsm$o3o3e$-berlin.de...
    >
    > "Jakob Bieling" <> wrote in message
    > news:bgeh8q$b9n$00$-online.com...
    > > "John Harrison" <> wrote in message
    > > news:bgegh7$o6ne7$-berlin.de...
    > > >
    > > > "Tobias Langner" <> wrote in message
    > > > news:bgef9i$1pt$05$-online.com...
    > > > > compiling this line:
    > > > >
    > > > > SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper,

    > RefCount>(new
    > > > > LongWrapper(1));
    > > > >
    > > > > with SmartPtr being:
    > > > >
    > > > > template <class T, template <class> class OwnershipPolicy>
    > > > > class SmartPtr : public OwnershipPolicy<T> {
    > > > > public:
    > > > > explicit SmartPtr(T* pointer);
    > > > > SmartPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > > > > SmartPtr<T, OwnershipPolicy> &operator=(SmartPtr<T,

    OwnershipPolicy>
    > > &);
    > > > > SmartPtr<T, OwnershipPolicy> &operator=(T*);
    > > > > ~SmartPtr();
    > > > > bool isNULL() { return pointer_==NULL; }
    > > > > T& operator*() const;
    > > > > T* operator->() const;
    > > > >
    > > > >
    > > > > private:
    > > > > void deleteObject();
    > > > > void copyPtr(SmartPtr<T, OwnershipPolicy>& otherPointer);
    > > > > void copyPtr(T* otherPointer);
    > > > > SmartPtr();
    > > > > T* pointer_;
    > > > >
    > > > > };
    > > > >
    > > > > i get the following error:
    > > > >
    > > > > test.cpp:22: no matching function for call to `SmartPtr<LongWrapper,
    > > > > RefCount>::SmartPtr(SmartPtr<LongWrapper, RefCount>)'

    smartptr.h:105:
    > > > > candidates are: SmartPtr<T, OwnershipPolicy>::SmartPtr(SmartPtr<T,
    > > > > OwnershipPolicy>&) [with T = LongWrapper, OwnershipPolicy =

    RefCount]
    > > > >
    > > > > wich I don't understand.
    > > >
    > > > Compiles fine for me once I've added dummy definitions for LongWrapper

    > and
    > > > RefCount. Post minimal, compilable code please.

    > >
    > >
    > > Which compiler did you use? I did not compile it, since I was too

    laze
    > > to add the dummy definitions, but as far as I can tell, it should not
    > > compile. See my reply to the OP.
    > >

    >
    > Right, I used that well known compiler which doesn't complain about
    > references to temporaries, VC++. Since I just upgraded to 7.1, I was

    hoping
    > that was fixed. I guess not.



    It was. It is a so called 'languag extension'. Enable it, and you will
    get the error, too. But then, unfortunately, the windows headers will not
    compile anymore. I think the STL will, though.

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Aug 1, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    Error message I don't understand

    simon, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    321
    Hatim R
    Dec 5, 2003
  2. m miller

    don't understand error

    m miller, Jan 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,749
    smitdso
    Sep 8, 2006
  3. Wayne  Wengert

    Don't Understand This error?

    Wayne Wengert, Mar 3, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    1,147
    George Christopher
    Jan 4, 2006
  4. Wayne Wengert

    Don't Understand error

    Wayne Wengert, Nov 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    474
    Wayne Wengert
    Dec 4, 2005
  5. Chris Saunders

    Don't understand syntax error

    Chris Saunders, May 16, 2005, in forum: C Programming
    Replies:
    9
    Views:
    498
    CBFalconer
    May 17, 2005
Loading...

Share This Page