compiler error I don't understand

T

Tobias Langner

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.
 
J

John Harrison

Tobias Langner said:
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
 
J

Jakob Bieling

Tobias Langner said:
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
 
J

Jakob Bieling

John Harrison said:
Tobias Langner said:
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
 
T

Tobias Langner

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


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?
 
J

Jakob Bieling

Tobias Langner said:
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
 
T

Tobias Langner

Jakob said:
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.
 
J

Jakob Bieling

John Harrison said:
Jakob Bieling said:
John Harrison said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top