![]() |
C++ und Delphi -> Rekursiv Problem
Hallo, habe hier ein riesen Problem. Irgendwie versteht mein C++ Code nicht was ich will.
Ich wollte eine bestimmte Funktion von Delphi nach C++ portieren. Die Delphi Funktion funktioniert einwandfrei. Also zu der Funktion. Ich habe eine DB, in der einige Informationen gepseichert sind. Ähnlicher Aufbau wie eine Baumstruktur. Nun wollte ich das auslesen. "LoadNodes" gibt von einer bestimmten ID die anzahl der Sub Knoten wieder. "ReadNode" liest nur die Werte des Sub Knotens aus. "FLoadNodes" wird nur für die rekursion benötigt. Ich starte die Funktion FLoadNodes in C++ und übergebe ID = -1. Dann liest er den ersten baum einwandfrei aber sobald die rekursion einsetzt, sind alle werte von i völlig Falsch. Beispiel: Root |- Test1 |- Test11 |- Test111 |- Test12 |- Test13 |- Test2 |- Test21 |- Test22 Delphi liest das wunderbar aus. Bei C++ bekommt der nach dem Lesen von Test111 zum sprung in Test12 ein Problem. Die Variable i für den inkrement wird auf einen utopischen Wert gesetzt. Aber erst, sobald die FLoadNodes selber das erstemal beendet wurde durch die Bedingung. Kann dass sein, dass die Variablen nicht lokal sind sondern global werden? Hier die Funktionierende Delphi Funktion.
Delphi-Quellcode:
Dies wollte ich Portieren nach C++:
function FLoadNodes(id: integer): integer;
var i: integer; c: integer; res: PNavRes; begin new(res); i := -1; repeat inc(i); c := LoadNodes(id, 1); if (i < c) then if (ReadNode(i, res)) then begin FLoadNodes(res^.ID); // open recursive the function if sub notes are available from this node end; c := LoadNodes(id, 1); until (c = 0) or (i >= c); end;
Delphi-Quellcode:
Habt Ihr eine Idee?
void FLoadNodes(int id){ // this function load the nodes in recursive methode
int i; int c; i = -1; do { i++; c = LoadNodes(id, 1); if (i < c){ if (ReadNode(i, &NavRes) == true){ FLoadNodes(NavRes.ID); // open recursive the function if sub notes are available from this node } } c = LoadNodes(id, 1); } while( (c == 0) || (i >= c) ); } THX |
Re: C++ und Delphi -> Rekursiv Problem
Moin clues,
lass mal den expliziten Vergleich auf true weg, also statt
Code:
nur
(ReadNode(i, &NavRes) == true)
Code:
oder
(ReadNode(i, &NavRes))
Code:
((bool)ReadNode(i, &NavRes))
|
Re: C++ und Delphi -> Rekursiv Problem
Leider nicht.
Bei allen 3, gleiche symptome. |
Re: C++ und Delphi -> Rekursiv Problem
Ich habe mal hier den kompletten code mit angefügt, der in C geschrieben ist.
Kann das vieleicht auch daran liegen, dass ich die i variable in eine Funktion übergebe die in einer Delphi Dll drinne liegt?
Code:
Hier sind die Delphi Deklarationen von der DLL. Vieleicht könnt ihr Irgend etwas sehen
#include <windows.h>
#include <stdio.h> #include <iostream> LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); char szClassName[ ] = "WindowsApp"; HWND hwnd; // ########################### NEEDED Global Variable for BTT ############################ typedef struct TNavRes { INT ID; LPTSTR txt; }; // Declare the structure of the result no TNavRes NavRes; // result of the node // Declare all dll functions typedef bool (*POPENDB)(LPTSTR Filename); // this function open the DB Connection typedef int (*PLOADNODES)(int id, int ix); // this function load the NodeList with ParentID number ID typedef int (*PNNN)(int id); // this function load the NodeList with ParentID number ID // if id < 0 then load all root nodes from DB // if id >= 0 then load all sub nodes from DB // result of the function is count of entrys typedef bool (*PREADNODE)(int Nr, TNavRes* res); // Read the node informations (entry) // Declare the globale variable of the functions POPENDB OpenDB; PLOADNODES LoadNodes; PREADNODE ReadNode; PNNN nnn; // Declare the needed globale variables HINSTANCE dllhwnd; // This is the handle for the BTT - DLL // ---------------------------------------------------------------------------------------- /* for (i = 0; i < LoadNodes(id, 1); i++){ if (ReadNode(i, &NavRes)){ // LPTSTR buf; // sprintf(buf, "%i", i); // writeln(buf); // Saving now the result in a treeview // NavRes.ID <= ID of the entry in the DB (UNIQUE) // NavRes.txt <= Text of the entry in the DB for the caption of the node in a treeview FLoadNodes(NavRes.ID); // open recursive the function if sub notes are available from this node } } */ char* itoa(int input){ static char buffer[16]; snprintf(buffer,sizeof(buffer),"%d",input); return buffer; } void FLoadNodes(int id){ // this function load the nodes in recursive methode int i; int c; i = -1; // nnn(id); do { i++; c = LoadNodes(id, 1); if (i < c){ if ((bool)ReadNode(i, &NavRes)){ FLoadNodes(NavRes.ID); // open recursive the function if sub notes are available from this node } } c = LoadNodes(id, 1); } while( (c == 0) || (i >= c) ); } void FOpenDB(){ char buf[255]; if (dllhwnd != NULL){ OpenDB = (POPENDB) GetProcAddress(dllhwnd, "OpenDB"); LoadNodes = (PLOADNODES) GetProcAddress(dllhwnd, "LoadNodes"); ReadNode = (PREADNODE) GetProcAddress(dllhwnd, "ReadNode"); nnn = (PNNN) GetProcAddress(dllhwnd, "nnn"); if (OpenDB != NULL){ if (OpenDB("C:\\Dokumente und Einstellungen\\Steffen\\Desktop\\BTT.btt") == true){ SetWindowText(hwnd, "Connected"); if (LoadNodes != NULL){ FLoadNodes(-1); // Load now the informations for the treeview starts at root } } else { SetWindowText(hwnd, "not Connected"); } } } } int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { MSG messages; WNDCLASSEX wincl; wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof (WNDCLASSEX); wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; if (!RegisterClassEx (&wincl)) return 0; hwnd = CreateWindowEx ( 0, szClassName, "Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL ); ShowWindow (hwnd, nFunsterStil); dllhwnd = LoadLibrary("E:\\Programmieren\\SitelTools\\BTT\\bttdll\\btt.dll"); // load now the BTT library mapping in the memory FOpenDB(); while (GetMessage (&messages, NULL, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); } FreeLibrary(dllhwnd); // give free the BTT library mapping in the memory return messages.wParam; } LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }
Delphi-Quellcode:
function CloseDB(): boolean; StdCall;
function ReadAddInfo(ID: integer; Res: PChar): boolean; StdCall; function ReadNode(Nr: integer; Res: PNavRes): boolean; StdCall; function LoadNodes(ID: integer; ix: integer): integer; stdcall; function OpenDB(FileName: PChar): boolean; stdcall; |
Re: C++ und Delphi -> Rekursiv Problem
Deine Abbruchbedingung ist komplett falsch. Da gehört dringend ein ! davor, weil repeat ausführt bis Bedingung, do-while aber solange die Bedingung wahr ist.
Ich denke, die Funktion müsste so aussehen:
Code:
int FLoadNodes(int ID) {
int i, c; TNavRes* res; new(res); i = -1; do { i++; c = LoadNodes(id, 1); if (i < c) { if (ReadNodes(i, res)) { FLoadNodes(res->ID); } } } while (! ((c == 0) || (i >= c))); } |
Re: C++ und Delphi -> Rekursiv Problem
Bin ich blöd (:stupid:) oder ist
Delphi-Quellcode:
eigentlich das genaue Gegenteil von
repeat
until (c = 0) or (i >= c);
Code:
Schleife Eins wiederholt alles solange, bis c=0 ist, oder i >= c ist.
do
while( (c == 0) || (i >= c) ); Schleife Zwei aber wiederholt alles solange, WÄHREND c=0 ist, oder i>=c ist. Also beim Delphi-Code ist die Abbruchbedingung, dass c = 0 bzw. i >=c ist, und beim C++-Code ist die Weitermachbedingung, dass c = 0 bzw. i >= c ist. Isses das? :stupid: Wenn ja, dann mach aus dem C++-Code besser
Code:
;)
do
while( (c!=0) || (i < c) ); //edit: *ARGH* Zu langsam. -.- Aber ausführlicher :P |
Re: C++ und Delphi -> Rekursiv Problem
Ok , dank dir erstmal, hat geholfen.
ich habe den code mal ganz kleines bischen abgewandelt. Aber ist genau dein Code. was ist nun aber anders an dem Code? Functionierender Code:
Code:
der Code funktioniert aber net
int FLoadNodes(int id) {
int i; int c; i = -1; do { i++; c = LoadNodes(id, 1); if (i < c) { if (ReadNode(i, &NavRes)) { FLoadNodes(NavRes.ID); } } } while (! ((c == 0) || (i >= c))); }
Code:
Warum muss ich negieren? ist doch eine Austrittsbedingnug? So ist es zumindestens in Delphi.
int FLoadNodes(int id){ // this function load the nodes in recursive methode
int i; int c; i = -1; do { i++; c = LoadNodes(id, 1); if (i < c){ if ((bool)ReadNode(i, &NavRes)){ FLoadNodes(NavRes.ID); // open recursive the function if sub notes are available from this node } } c = LoadNodes(id, 1); // <-- ist diese Zeile schuld? }while(! ((c == 0) || (i >= c))); } Mein Problem ist erstmal gelöst. THX. Ich hoffe aber trotzdem auf Aufklärung, will ja nicht dumm sterben :). |
Re: C++ und Delphi -> Rekursiv Problem
Liste der Anhänge anzeigen (Anzahl: 1)
So ich habe mal ein Fehlerbeispiel angefügt.
Diese Loginformation kommt von der Delphi Dll. Die Werte die den Funktionen übergeben wurden sind hier aufgeführt. Die ersten 3 Zahlen stehen für. [ID][i][c] danach kommt die Funktioner mit den Parametern. Bei der letzten wo der Fehler auftritt kam aber halt die Zugriffsverletzung, da ja auch kein Wert in der DB dafür gibt. Eigendlich müsste das aber auch weiter gehen, aber wie kommt diese Zahl zustande? |
Re: C++ und Delphi -> Rekursiv Problem
Schei.. Problem noch nicht gelösst. Mir ist gerade eben aufgefallen, dass nun die komplette Anwenung hängen bleibt, denke mal Unendlichkeitsschleife.
|
Re: C++ und Delphi -> Rekursiv Problem
Sorry, ich hab vergessen eine Zeile zu übersetzen ;) Wenns ohne die Zeile funktioniert, liegts wohl an der einen ^^
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:29 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz