Why do I need to convert a string to a string for Fl_Input

23 views
Skip to first unread message

roger tunnicliffe

unread,
Oct 5, 2022, 6:40:12 PM10/5/22
to fltk.general
I have the following definitions:-

 Fl_Input            *win2_unitprice;
  win2_unitprice        = new Fl_Input(x+50,y+200,100,20,"Unit Price..:");

and the following code:-

double z;
.
.
  string s1 = to_string(z);
  string s2 = s1.c_str();
// win2_unitprice->value(s1);
    win2_unitprice->value(s1.c_str());

If I uncomment line 3 I get the following error:-
error: no matching function for call to ‘Fl_Input::value(std::__cxx11::string&)’
   win2_unitprice->value(s1);

but if i look at the data I see that:-
s1 holds the address of the data 0x31 0x2e 0x31 0x30 ....
and 
s2 holds the address of the data 0x31 0x2e 0x31 0x30 ....

so why do I need to use c_str() to make this work ??

Breakpoint 1, w2_browser_Callback (w=0x7dc1b0, data=0x0) at fV3.04.cxx.267
(gdb) x/8b &s1
0x7fffffffdc30:             0x40 0xdc 0xff 0xff 0xff 0x7f 0x00 0x00
(gdb) x/8b 0x7fffffffdc40
0x7fffffffdc40:                0x31 0x2e 0x31 0x30 0x30 0x30 0x30 0x30
(gdb) x/8b &s2
0x7fffffffdc50:                0x60 0xdc 0xff 0xff 0xff 0x7f 0x00 0x00
(gdb) x/8b 0x7fffffffdc60:
0x7fffffffdc60:                0x31 0x2e 0x31 0x30 0x30 0x30 0x30 0x30

Mo_Al_

unread,
Oct 6, 2022, 7:08:09 AM10/6/22
to fltk.general
The C++ standard library doesn't have an implicit conversion method from std::string to const char pointer, which is a good thing since std::string doesn't really wrap a pointer to a constant nul-terminated char sequence, it wraps a char array (which might be heap-allocated, if SSO doesn't kick in) along with capacity and length. 

There's also the point that for libraries, you'd generally want to avoid exposing std types in your public api. If a library is built with libstdc++ (default with gcc), that means it will fail to link with another built with libcxx (default with clang), and vice versa.

Ian MacArthur

unread,
Oct 6, 2022, 8:32:36 AM10/6/22
to fltk.general
On Wednesday, 5 October 2022 at 23:40:12 UTC+1 roger wrote:
 
  string s1 = to_string(z);
  string s2 = s1.c_str();
// win2_unitprice->value(s1);
    win2_unitprice->value(s1.c_str());

If I uncomment line 3 I get the following error:-
error: no matching function for call to ‘Fl_Input::value(std::__cxx11::string&)’
   win2_unitprice->value(s1);

but if i look at the data I see that:-
s1 holds the address of the data 0x31 0x2e 0x31 0x30 ....
and 
s2 holds the address of the data 0x31 0x2e 0x31 0x30 ....

so why do I need to use c_str() to make this work ??

Not at all clear what you think s2 is doing here? You declare and set it but do not appear to use it?

In any case, the "answer" to your question is that Fl_Input::value() does not take a "string" in the C++ sense, it takes a pointer to an array of characters; which is often referred to as a "string" in the C-sense, but is a different animal.

The fltk API was (in large part, anyway) originally inherited form a previous, proprietary, lib called "Forms" which was a C lib, and presented a C API. In consequence, much of fltk also presents a fairly C-like API. Which often aids portability, but may seem archaic now...

A C++ "string" is an object which holds a string of text; the actual content is notionally opaque, but it probably does hold an array of characters, along with various attributes about the string, such as length and so on, and methods to manipulate that data.

A C "string" is just a sequence of bytes that ends in a zero - you don't even know how long it is unless you count 'em.

The C++ object is a much less error-prone entity, but that's a whole other story.
Welcome to the world of C-programming...

Philip Rose

unread,
Oct 6, 2022, 3:33:21 PM10/6/22
to fltkg...@googlegroups.com

From: roger tunnicliffe
Sent: 05 October 2022 23:40
To: fltk.general
Subject: [fltk.general] Why do I need to convert a string to a string for Fl_Input

 

I have the following definitions:-

 

 Fl_Input            *win2_unitprice;
  win2_unitprice        = new Fl_Input(x+50,y+200,100,20,"Unit Price..:");

and the following code:-

double z;

.

.
  string s1 = to_string(z);
  string s2 = s1.c_str();
// win2_unitprice->value(s1);
    win2_unitprice->value(s1.c_str());

If I uncomment line 3 I get the following error:-
error: no matching function for call to ‘Fl_Input::value(std::__cxx11::string&)’
   win2_unitprice->value(s1);

but if i look at the data I see that:-

s1 holds the address of the data 0x31 0x2e 0x31 0x30 ....

and 

s2 holds the address of the data 0x31 0x2e 0x31 0x30 ....

 

so why do I need to use c_str() to make this work ??


<START>

Hi Roger,

 

The short answer is the difference between strings in C and C++ languages. A C-string has type char* - that is a pointer to a series of 8-bit characters in memory, ended with the NUL (x00) character. A C++-string (type string) is a class with some overheads to manage access to it and memory allocation.

 

FLTK generally uses char* to pass string data around.

 

The class string method string::c_str() accesses the raw char* string within the class and this is used to pass the string content to FLTK widgets.

 

The statement

string s2 = s1.c_str()

uses a feature of C++ that overloads the assignment operator (=) so that you can create a new C++ string from the C string.

 

Regards Phil.

roger tunnicliffe

unread,
Oct 6, 2022, 5:29:13 PM10/6/22
to fltk.general
Thx for the replies everybody. No sure I really "get it" but at least I know how to make it work. (s2 existed so I could look at the values in GDB)

Having said that, how should I code this so iy happens in one step?

string s1 = to_string(z);
win2_unitprice->value(s1.c_str());

imm

unread,
Oct 6, 2022, 5:48:34 PM10/6/22
to General FLTK
On Thu, 6 Oct 2022, 22:29 roger wrote:
Thx for the replies everybody. No sure I really "get it" but at least I know how to make it work. (s2 existed so I could look at the values in GDB)

Having said that, how should I code this so iy happens in one step?

string s1 = to_string(z);
win2_unitprice->value(s1.c_str());

There are possibly as many answers to that as there are programmers... Opinions will vary!

FWIW I'd be using C-strings & snprintf() probably. That's not the answer you need or want.

The easiest way might be to use an Fl_Value_Input widget instead, since that will accept your double z directly without conversions at all.
--
Ian
From my Fairphone FP3
   

Mo_Al_

unread,
Oct 6, 2022, 5:49:27 PM10/6/22
to fltk.general
win2_unitprice->value(to_string(z).c_str());

should work

roger tunnicliffe

unread,
Oct 6, 2022, 7:49:10 PM10/6/22
to fltk.general
Thx May64

That does indeed work.
Cheers
Roger
Reply all
Reply to author
Forward
0 new messages