By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

compiler error I don't understand

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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
Jul 19 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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)
Jul 19 '05 #3

P: n/a
"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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)
Jul 19 '05 #4

P: n/a
Jakob Bieling wrote:
"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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?
Jul 19 '05 #5

P: n/a
"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-online.com...
Jakob Bieling wrote:
"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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)
Jul 19 '05 #6

P: n/a
Jakob Bieling wrote:
"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-online.com...
Jakob Bieling wrote:
> "Tobias Langner" <to************@t-online.de> wrote in message
> news:bg*************@news.t-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.
Jul 19 '05 #7

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bg*************@news.t-online.com...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Tobias Langner" <to************@t-online.de> wrote in message
news:bg*************@news.t-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)
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.