[Click] strange string
Eddie Kohler
kohler at cs.ucla.edu
Wed Apr 5 01:32:17 EDT 2006
Hi Koen,
Szymon is correct. The problem is that C++ already has a meaning for
the operation
const char * + int
-- it's the same as any pointer arithmetic! Thus,
_dump->add_detailed_info("Hello with xid=" + 5);
will behave like
_dump->add_detailed_info(" with xid=");
You trigger String behavior by casting one or more of the operands to
String type. In your case it looks like your "_info->get_network_id
()" arguments are returning type char. That is too bad, since those
characters will be interpreted as CHARACTERS, not as integers. (So
value 65 will appear as 'A'.)
Eddie
On Apr 4, 2006, at 5:04 PM, Koen Segers wrote:
> 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
> _______________________________________________
> click mailing list
> click at amsterdam.lcs.mit.edu
> https://amsterdam.lcs.mit.edu/mailman/listinfo/click
More information about the click
mailing list