Hello everybody,
The thing is that I need to create a list data structure, so I tried the INLINE command. But I only know a little something in C and less than nothing (if possible!) in C++. So, I just gave it a shot with the former method; I thought it should have been fine since I had been using a C++ compiler in my C courses.
Unfortunately, there is a problem when I'm trying to include a(ny) header file to grant me access to the proper malloc function. I have reasons to believe the problem lies somewhere herein:
· the only source code (to make things easy to the eye):
INLINE
#include <stdlib.h>
ENDINLINE
· results window:
compiling:
In file included from D:\DOCUME~1\akis\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:15:
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h: In function `int __GLBASIC__::__MainGameSub_()':
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:61: error: expected unqualified-id before string constant
*** FATAL ERROR - Please post this output in the forum
· by double-clicking on the pre-last line of the results window above opens a new tab titled "stdlib.h", with the arrow pointing at the second line below, somewhere among the so many lines of the stdlib header file:
#ifdef __cplusplus
extern "C" {
#endif
Of course I'm not acquainted with any details regarding header files; I merely know how to use some of the functions they provide.
I'm, by no means, asking for help with the whole process of creating the list, just if I could create it using the INLINE command and C-like notion but with the proper approach (which I obviously lack!).
If you think you could spare me a few of your thoughts, it'd be appreciated!
You'll find that all INLINE stuff has to go into a seperate GBAS file, for some reason, but thats not too bad really.
With headers, you have to be careful : There are no header files (by default) present - there are some for Windows, Linux and the GP32X around (I think its availiable from the main website) though - but I think these are only the OS's API calls
So, you have two options :
A) Download the headers and hope stdlib is included, or
B) Dont worry about it...
With malloc you should just be able to use :
extern "C" void *malloc(int);
which is approximately the correct prototype for it.
Ironically, it's been something I've been thinking about adding - and I've just started it :) See http://www.glbasic.com/forum/index.php?topic=2853.0 (http://www.glbasic.com/forum/index.php?topic=2853.0)
Hello!
What sort of list are you trying to create? Is it something similar to the TYPE command together with a DIM?
Like this:
TYPE a_list
name$
score%
ENDTYPE
GLOBAL the_list[] AS a_list // Space for the list
GLOBAL a_person AS a_list // A single object in the list needed for entering data
// Enter the information into the list
a_person.name$ = "Moru"
a_person.score = 100
DIMPUSH the_list[], a_person
a_person.name$ = "Someone else"
a_person.score = 45
DIMPUSH the_list[], a_person
// Display the list
FOREACH a_person IN the_list[]
DEBUG "Name: " + a_person.name$ + " "
DEBUG "Score: " + a_person.score + "\n"
NEXT
instead of malloc, you can try the C++ keyword "new":
char* pC = new char[1234];
...
delete [] pC;
Is there really need for INLINE in your case?
Quote from: MrTAToad on 2009-Mar-02
You'll find that all INLINE stuff has to go into a seperate GBAS file, for some reason, but thats not too bad really.
With headers, you have to be careful : There are no header files (by default) present - there are some for Windows, Linux and the GP32X around (I think its availiable from the main website) though - but I think these are only the OS's API calls
So, you have two options :
A) Download the headers and hope stdlib is included, or
B) Dont worry about it...
With malloc you should just be able to use :
extern "C" void *malloc(int);
which is approximately the correct prototype for it.
Ironically, it's been something I've been thinking about adding - and I've just started it :) See http://www.glbasic.com/forum/index.php?topic=2853.0 (http://www.glbasic.com/forum/index.php?topic=2853.0)
I had already downloaded the header files from the website and had placed them to their respective directories. That's why I was so much (badly) surprised with the program's reaction!
Anyway, I think I will have to study the contents of your link more exhaustively!
But hey, thanks for replying so soon!
" #include <stdlib.h> " Includes windows.h, there is a problem to include it on GLBasic (double-declaration!).
Quote from: Moru on 2009-Mar-02
Hello!
What sort of list are you trying to create? Is it something similar to the TYPE command together with a DIM?
Like this:
TYPE a_list
name$
score%
ENDTYPE
GLOBAL the_list[] AS a_list // Space for the list
GLOBAL a_person AS a_list // A single object in the list needed for entering data
// Enter the information into the list
a_person.name$ = "Moru"
a_person.score = 100
DIMPUSH the_list[], a_person
a_person.name$ = "Someone else"
a_person.score = 45
DIMPUSH the_list[], a_person
// Display the list
FOREACH a_person IN the_list[]
DEBUG "Name: " + a_person.name$ + " "
DEBUG "Score: " + a_person.score + "\n"
NEXT
Yes, that's pretty much the method I've already implemented. But the array is 'inconvenient' in that the new items are to be placed in its beginning which results in a shift-towards-the-end of all the other many items. Additionally, when you add a new item to a list, the list remains sorted-something that I'll be needing in the near future, I'm afraid!
It's not that I'm not having a good result on my current project this way, but why not improving the code if only to make it go softer on the CPU? Think it's crazy? It's just a notion that's been instilled into me!
I really appreciate your going into the trouble of even writing an example!
Quote from: Kitty Hello on 2009-Mar-02
instead of malloc, you can try the C++ keyword "new":
char* pC = new char[1234];
...
delete [] pC;
Is there really need for INLINE in your case?
C++ keyword, hee..? Haven't been there yet!
But, I tried what you proposed and it seems to be working more than just fine! I even combined it with a struct and I got myself a list (on a rather primitive stage, yes, but it's a start, nonetheless)!
Thank you for sharing some of your time (more like a fraction of it!). I couldn't have hoped for an even shorter, simpler and, at the same time, effective response!
By the way, do you think that I should be avoiding the INLINE command in general?
Quote from: Schranz0r on 2009-Mar-02
" #include <stdlib.h> " Includes windows.h, there is a problem to include it on GLBasic (double-declaration!).
Huh? And how would someone get past this, then?
Thanks for the thought.
Hopefully, setting the WINDOWS_MEAN_AND_LEAN flag would only include those that are in Kitty's download...
Quoteinstead of malloc, you can try the C++ keyword "new"
But I like malloc!
If you like malloc, do this:
#define malloc(a) ((void*)new char[(int)(a)])
#define free(a) {delete[] (a)}
INLINE is not bad generally. You just have to be sure what you do, since platform specific stuff will break cross compiling. And usually you can do everything you want in GLBasic, too.
If you want sorting, the command SORTARRAY can sort an array.
Surely, there might be problems, where a linked list is better. But usually a dynamic array performs better.
In this case, there is actually a good reason to not to use malloc/free : reducing the number of prototypes.