[Click] strange string

Koen Segers koen.segers at edpnet.be
Tue Apr 4 20:04:08 EDT 2006


On Wednesday 05 April 2006 01:40, you wrote:
> Hi Koen,
>
> > _dump->add_detailed_info("Hello with xid=" + _info->get_network_id() + "
> > and seqnr=" + _info->get_broadcast_seqnr() + " and size=" +
> > _info->get_network_size());
> >
> > Compiling this gives:
> > elements/pdhcp/pdhcp_hello_generator.cc:105: error: invalid operands of
> >    types `const char*' and `const char[12]' to binary `operator+'
>
> This is just how C++ works. In your call, the first operand is const
> char[] and it cannot be implicitly cast to a String (how would the
> compiler know this?). Try telling the compiler to do the cast:

Why can't the compiler know this? I thought every const char* had a default 
end marker. 

>
> add_detailed_info(String("Hello with xid=") + _info->get_network_id() 
> //...
>
> I didn't compile this, but it should work.


What you are trying to do is making the first element a string so that the 
operator+ is used. It seems that this makes operator+ ambigue:

./elements/pdhcp/pdhcp_hello_generator.cc:105: error: ISO C++ says that `
   Click::String Click::operator+(Click::String, char)' and `operator+' are 
   ambiguous even though the worst conversion for the former is better than 
the 
   worst conversion for the latter

I believe my xid parameter is casted to a char. Why char?

I casted all my uint32_t and int values to String:
_dump->add_detailed_info(String("Hello with xid=") + (String)
(_info->get_network_id()) + " and seqnr=" + (String)
(_info->get_broadcast_seqnr()) + " and size=" + (String) 
(_info->get_network_size()));

This gives the desired output.

I can't believe this is the normal way of programming with strings.

>
> On 4/4/06, Koen Segers <koen.segers at edpnet.be> wrote:
> > Hi,
> >
> > I just found something strange.
> > I've created a function called add_detailed_info that has a String
> > parameter. When I call this function in the following manner, it doesn't
> > compile.
>
> ///
>
> > When I change it to the following, all works wonderful.
> > String str = "Hello with xid=";
> >                 str += _info->get_network_id();
> >                 str += " and seqnr=";
> >                 str += _info->get_broadcast_seqnr();
> >                 str += " and size=";
> >                 str += _info->get_network_size();
> >                 _dump->add_detailed_info(str);
> >
> >
> > Now the question is, how is this possible?
> > I saw that the string class has overloaded the operator+ for many cases.
> > Is there a case missing for what I'm trying to do? Or maybe the
> > operator()?
> >
> > xid is a uint32_t
> > broadcast_seqnr = int
> > network_size = int
> >
> > greetz
> > --
> >
> > Koen Segers

-- 

Koen Segers
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : https://amsterdam.lcs.mit.edu/pipermail/click/attachments/20060404/1c7513e0/attachment.pgp


More information about the click mailing list