Making a tree-like structure using user-defined types and set?

Started by Quin, January 27, 2025, 09:57:01 PM

Previous topic - Next topic

Quin

MrBcx,
This works great, thank you! However, is it possible for me to declare my array using Set...End Set, rather than having to dimension and access all the elements manually when having nested dynamic arrays like this? If so, how?
Thanks.

MrBcx

Quote from: Quin on January 28, 2025, 10:39:54 AM
MrBCX,
I did that, and am pretty sure the solution would be changing the type to Node Ptr. However, when I try that, I get errors like this:
warning C4200: nonstandard extension used: zero-sized array in struct/union 
Would I want to use a dynamic array? I'm not sure why, but arrays in BCX are the one thing that I've really had issues adapting to from other languages.

The following comes straight out of BCXHelp section TYPE-END TYPE.

It compiles and runs fine in BED using any of the listed compilers on the Build dlgbox

It should get you pointed in the right direction.



TYPE First
    a$
    b%
END TYPE

TYPE Second
    c%
    DYNAMIC Something[] AS First
END TYPE

DIM DYNAMIC Third[6] AS Second
DIM i, ii

FOR ii = 0 TO 5
    REDIM Third[ii].Something[10]
    FOR i = 0 TO 9
        Third[ii].Something[i].b% = i
        Third[ii].Something[i].a$ = "this is Something" + _
        STR$(i) + _
        " of Third" + _
        STR$(ii)
    NEXT
NEXT

FOR ii = 0 TO 5
    FOR i = 0 TO 9
        ? Third[ii].Something[i].b%
        ? Third[ii].Something[i].a$
    NEXT
NEXT

FOR ii = 0 TO 5
    FREE Third[ii].Something
NEXT

FREE Third

PAUSE


Quin

MrBCX,
I did that, and am pretty sure the solution would be changing the type to Node Ptr. However, when I try that, I get errors like this:
warning C4200: nonstandard extension used: zero-sized array in struct/union 
Would I want to use a dynamic array? I'm not sure why, but arrays in BCX are the one thing that I've really had issues adapting to from other languages.

MrBcx

Quin,

I'm not going to sink time in this but what I will recommend is

1) Paste only this code into BED

Type Node
    Name As string
    Children[] As Node
End Type


2) Attempt to compile it and observe the errors.


Morale of the story ... building a house starts with the foundation.  A faulty foundation results in a faulty house.

Quin

For an application, I need a tree-like structure, where each node has a name, and optionally a list of other children they support. I've tried compiling this code with MSVC UCRT on Windows 10 and 11 IoT lTSC 2024, with no luck. Would someome mind pointing me in the right direction?
Type Node
    Name As string
    Children[] As Node
End Type

Set Test[] As Node
    "First node", {
        "First child", {},
        "Second child", {}
    },
    "Second node", {
        "Third child", {},
        "Fourth child", {}
    }
End Set
Dim i As Integer
For i = 0 To Ubound(Test)
    ? Str$(i)
Next

BCX BASIC to C/C++ Translator (c) 1999-2025 by Kevin Diggins
Version 8.2.0 (12/27/2024) Compiled using MS Visual C++ (194033811) for 64-bit Windows
[Lines In: 19] [Lines Out: 386] [Statements: 9] [Time: 0.03 Sec's]
BCX translated [Test.Bas] to [Test.C] for a C Compiler

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.11.4
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
**************************************************************************

MSVC is compiling [ "C:\Users\Quin\git\2dSetTest\Test" ] as a 64-bit CONSOLE application.
Test.c
C:\Users\Quin\git\2dSetTest\Test.c(285): error C2460: '_Node::Children': uses '_Node', which is being defined
C:\Users\Quin\git\2dSetTest\Test.c(282): note: see declaration of '_Node'
C:\Users\Quin\git\2dSetTest\Test.c(306): error C2233: 'Test': arrays of objects containing zero-size arrays are illegal
C:\Users\Quin\git\2dSetTest\Test.c(308): error C2440: 'initializing': cannot convert from 'initializer list' to 'int'
C:\Users\Quin\git\2dSetTest\Test.c(308): note: The initializer contains too many elements
C:\Users\Quin\git\2dSetTest\Test.c(312): error C2440: 'initializing': cannot convert from 'initializer list' to 'int'
C:\Users\Quin\git\2dSetTest\Test.c(312): note: The initializer contains too many elements

**************************************************************************
ERROR: Build failed
**************************************************************************
Thanks as always all!