Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template : cannot convert parameter 1 from const * to *const & ?

Reply
Thread Tools

Template : cannot convert parameter 1 from const * to *const & ?

 
 
nassim.bouayad.agha@gmail.com
Guest
Posts: n/a
 
      05-23-2007
Hello,
here is a code snippet showning my problem :

template<typename _K>
class TClass1
{
public:
void Process(const _K& arg) const
{

}
};
template<typename _I>
class TClass2
{
public:
void Process(const _I* & arg) const
{
_class1.Process(arg);
}
protected:
TClass1<_I*> _class1;

};
int main(int argc, char* argv[])
{

TClass2<int> test1;

int* ptr = new int;

test1.Process(ptr);

delete ptr;

return 0;
}

This code does not compile, I have got the following error message :
"cannot convert parameter 1 from 'const int *' to 'int *const & '"
Any idea?I have noted that if I declare _class1 as TClass1<const _I*>,
it is working...
Regards!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-23-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello,
> here is a code snippet showning my problem :


The first problem is all those underscores. Why do you think
you need them at all?

>
> template<typename _K>
> class TClass1
> {
> public:
> void Process(const _K& arg) const
> {
>
> }
> };
> template<typename _I>
> class TClass2
> {
> public:
> void Process(const _I* & arg) const
> {
> _class1.Process(arg);


'_I' is 'int', right? '_class1' then 'TClass1<int*>', right?
Then its template argument (the '_K' inside) is 'int*', right?
What's the specification of the 'Process' member, then?

void TClass1<int*>:rocess(int* const&);

So, when you're calling '_class1.Process', what argument type
are you giving? '_I const * &'. Considering '_I' is 'int',
you are trying to call 'Process' with

int const * &

instead of

int * const &

.. Decide on the position of the const. Or drop the * from
the definition of '_class1'.

> }
> protected:
> TClass1<_I*> _class1;
>
> };
> int main(int argc, char* argv[])
> {
>
> TClass2<int> test1;
>
> int* ptr = new int;
>
> test1.Process(ptr);
>
> delete ptr;
>
> return 0;
> }
>
> This code does not compile, I have got the following error message :
> "cannot convert parameter 1 from 'const int *' to 'int *const & '"
> Any idea?I have noted that if I declare _class1 as TClass1<const _I*>,
> it is working...


Right. If you add a const at the _I, the function you're trying to
call is

void TClass1<int const*>:rocess(int const* const&);

which is fine. Its argument ('int const* const&') is compatible with
'int const* &' you're trying to give it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
nassim.bouayad.agha@gmail.com
Guest
Posts: n/a
 
      05-24-2007
On 23 mai, 17:22, (E-Mail Removed) wrote:
> On 23 mai, 16:59, "Victor Bazarov" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > (E-Mail Removed) wrote:
> > > Hello,
> > > here is a code snippet showning my problem :

>
> > The first problem is all those underscores. Why do you think
> > you need them at all?

>
> > > template<typename _K>
> > > class TClass1
> > > {
> > > public:
> > > void Process(const _K& arg) const
> > > {

>
> > > }
> > > };
> > > template<typename _I>
> > > class TClass2
> > > {
> > > public:
> > > void Process(const _I* & arg) const
> > > {
> > > _class1.Process(arg);

>
> > '_I' is 'int', right? '_class1' then 'TClass1<int*>', right?
> > Then its template argument (the '_K' inside) is 'int*', right?
> > What's the specification of the 'Process' member, then?

>
> > void TClass1<int*>:rocess(int* const&);

>
> > So, when you're calling '_class1.Process', what argument type
> > are you giving? '_I const * &'. Considering '_I' is 'int',
> > you are trying to call 'Process' with

>
> > int const * &

>
> > instead of

>
> > int * const &

>
> > . Decide on the position of the const. Or drop the * from
> > the definition of '_class1'.

>
> > > }
> > > protected:
> > > TClass1<_I*> _class1;

>
> > > };
> > > int main(int argc, char* argv[])
> > > {

>
> > > TClass2<int> test1;

>
> > > int* ptr = new int;

>
> > > test1.Process(ptr);

>
> > > delete ptr;

>
> > > return 0;
> > > }

>
> > > This code does not compile, I have got the following error message :
> > > "cannot convert parameter 1 from 'const int *' to 'int *const & '"
> > > Any idea?I have noted that if I declare _class1 as TClass1<const _I*>,
> > > it is working...

>
> > Right. If you add a const at the _I, the function you're trying to
> > call is

>
> > void TClass1<int const*>:rocess(int const* const&);

>
> > which is fine. Its argument ('int const* const&') is compatible with
> > 'int const* &' you're trying to give it.

>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask- Masquer le texte des messages précédents -

>
> > - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -

>
> > - Afficher le texte des messages précédents -

>
> Thank you!- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -


Hello,
I still have a problem.The exemple that I gave is trivial,but how can
I do if I need to use this,for stl containers?For exemple,if I want a
multimap which key is a pointer on a type?!This may cause problems
because I need to store pointers to non const objects...
Any idea?
Thank you in advance.
Regards.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Re: Template : cannot convert parameter 1 from const * to *const & ? nassim.bouayad.agha@gmail.com C++ 0 05-23-2007 03:22 PM
Re: Template : cannot convert parameter 1 from const * to *const & ? nassim.bouayad.agha@gmail.com C++ 0 05-23-2007 03:21 PM
'atoi' : cannot convert parameter 1 from 'char' to 'const char *' John Smith C++ 6 10-31-2005 07:18 AM
cannot convert parameter 2 from 'const char *' to 'char *' Alfonso Morra C Programming 3 08-12-2005 01:41 PM



Advertisments