0

Strange narrowing conversion error from FLTK Color to Color Struct, Stroustrup's header Graph.h

Recently i have reached the chapter concerning Graphics in Stroustrup's book PPP2 , however i have faced unsettling _narrowing conversion_ error in Graph.h line 159 , after building and configuring the FLTK 1.1.10 library, the headers _dependant on FLTK_ provided by Stroustrup are in this Git Repo: https://github.com/MackSix/ppp-stroustrup.git . I have spent days trying to find a solution ,but so far i had no luck, i have searched the Web and posted the problem on StackExchange but no one has offered any solution, I have stopped coding completely because of this problem. The source code for the program is_ i use visual studio 2022 community with C++ 14 Standard : #include"../std_lib_facilities.h" #include "../Simple_window.h" #include "../Graph.h" int main() { using namespace Graph_lib; Point tl{ 100,100 }; // our graphics facilities are in Graph_lib // to become top left corner of window Simple_window win{ tl,600,400,"Canvas" }; // make a simple window Polygon poly; poly.add(Point{ 300,200 }); poly.add(Point{ 350,100 }); poly.add(Point{ 400,200 }); poly.set_color(Color::red); win.attach(poly); win.wait_for_button(); } The error message provided by the compiler is: Error C2397 conversion from 'Fl_Color' to 'unsigned long' requires a narrowing conversion pain C:\Users\abdua\source\repos\Graph.h 159 Any help would be much appreciated. Thank you in advance .

26th Apr 2024, 12:56 AM
Abdullah Othman
Abdullah Othman - avatar
20 Respostas
+ 3
the error with narrowing conversation i meet it too few times too but with other projects. You can solve it by changing the language version to C++17, otherwise you need to find the exact line cause the error and explicit cast the variable to ulong like this: myFunction((unsigned long)flColorVar);
18th May 2024, 11:31 PM
john ds
john ds - avatar
+ 2
Bob_Li but an attempt can be taken. Every bugs has their own troubleshooting in different systems. I understand your gratitude. Thanks for appreciations.
26th Apr 2024, 9:16 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 2
Bob_Li I tried that as well the function was one that take Fl_Color and return its equivalent int , but if I remember correctly that had resulted in the same error
26th Apr 2024, 9:49 AM
Abdullah Othman
Abdullah Othman - avatar
+ 1
Bob_Li unfortunately I tried that and I have the same narrowing conversion , this is really odd, I'm so confused right now
26th Apr 2024, 3:33 AM
Abdullah Othman
Abdullah Othman - avatar
+ 1
Abdullah Othman it seems like Graph.h have a bug from the message comment by Gregory Toepperwein https://groups.google.com/g/ppp-public/c/BtlzdWGuQpQ/m/KuDN4u-SPKgJ find the Graph.h file and change line 29 to: Color(unsigned int cc):c(Fl_Color(cc)), v(visible){} // v(visible) was originally v(vv)
26th Apr 2024, 3:40 AM
Bob_Li
Bob_Li - avatar
+ 1
@Bob_Li I did that and changed my fltk to latest version once again but now I have about 11 link errors just because I did this small change you suggested, my confusion and frustration about this problem went beyond my expectations, I simply give up this time, fltk is functional because I did the test code , however this is just getting out of hand believe me if I told you I have spent weeks on this problem and I'm tired of it . thanks for the comments tho I really appreciate it .
26th Apr 2024, 6:38 AM
Abdullah Othman
Abdullah Othman - avatar
+ 1
Why don't you create a helper function that converts Fl_Color to Graph_lib::Color? I think, FLTK uses a single integer to represent colors, whereas Stroustrup's library uses a struct to represent colors.
26th Apr 2024, 7:52 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 1
`нттp⁴⁰⁶ great suggestion, but after reading the Google group discussion, I think that the fix is not that simple. There seems to be bugs in other places, too. It had become more of an exercise in bug fixing and understanding compiler errors.
26th Apr 2024, 8:48 AM
Bob_Li
Bob_Li - avatar
+ 1
Abdullah Othman maybe you should take this as an opportunity to learn about debugging and updating source codes? Knowing how to fix broken code is an even more valuable skill than writing new code.
26th Apr 2024, 11:36 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li the thing is I'm a novice and for now just wanna learn basics from ppp3 and perhaps expand my knowledge gradually, but this is out of my scope for now, anyways I don't have many options because many chapters in the book are based on this header so it's hard to ignore.
26th Apr 2024, 11:44 AM
Abdullah Othman
Abdullah Othman - avatar
+ 1
The FLTK toolkit and C++ have progressed from since when the source code that was written 10 years ago. I would suggest learning from newer resources. Also, the repo you gave compiled for windows 7. That is really old. Expecting it to it compile for current Windows version without any problems is overly optimistic.
26th Apr 2024, 12:28 PM
Bob_Li
Bob_Li - avatar
0
The readme said the project requires FLTK v1.3.3
26th Apr 2024, 1:23 AM
Bob_Li
Bob_Li - avatar
0
I have used even more modern version namely 1.3.9 with the same error, I used and older version because I thought this may solve the problem, as the book was based on the same version I'm currently using.
26th Apr 2024, 1:25 AM
Abdullah Othman
Abdullah Othman - avatar
0
how about explicitly casting FL_Color to unsigned long when passing it to poly.set_color? poly.set_color((unsigned long)Color::red);
26th Apr 2024, 3:01 AM
Bob_Li
Bob_Li - avatar
0
could it be a problem with the IDE I'm using ?
26th Apr 2024, 3:34 AM
Abdullah Othman
Abdullah Othman - avatar
0
yeah, maybe it's just broken. I wonder if anybody got it to work. It's a 10 year old repo
26th Apr 2024, 7:24 AM
Bob_Li
Bob_Li - avatar
0
I have even searched for Stroustrup's email to notify him of this problem, If anyone has his email please share it with me
26th Apr 2024, 10:18 AM
Abdullah Othman
Abdullah Othman - avatar
0
Hello
27th Apr 2024, 12:16 PM
Олег Борисович
0
Hi I speak English and i also speak Spanish Im 13 years old and I want to be haker
30th Apr 2024, 9:13 PM
¿ANONYMUS?
¿ANONYMUS? - avatar
0
Que debo aprender primero python o html y css??
30th Apr 2024, 9:14 PM
¿ANONYMUS?
¿ANONYMUS? - avatar