AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

C++ und Delphi -> Rekursiv Problem

Ein Thema von clues1 · begonnen am 18. Sep 2005 · letzter Beitrag vom 18. Sep 2005
Antwort Antwort
Seite 1 von 2  1 2      
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#1

C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:02
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:
  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;
Dies wollte ich Portieren nach C++:
Delphi-Quellcode:
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) );
}
Habt Ihr eine Idee?
THX
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
Christian Seehase
(Co-Admin)

Registriert seit: 29. Mai 2002
Ort: Hamburg
11.105 Beiträge
 
Delphi 11 Alexandria
 
#2

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:15
Moin clues,

lass mal den expliziten Vergleich auf true weg, also statt

Code:
(ReadNode(i, &NavRes) == true)
nur

Code:
(ReadNode(i, &NavRes))
oder

Code:
((bool)ReadNode(i, &NavRes))
Tschüss Chris
Die drei Feinde des Programmierers: Sonne, Frischluft und dieses unerträgliche Gebrüll der Vögel.
Der Klügere gibt solange nach bis er der Dumme ist
  Mit Zitat antworten Zitat
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#3

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:27
Leider nicht.
Bei allen 3, gleiche symptome.
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#4

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:34
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:
#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;
}
Hier sind die Delphi Deklarationen von der DLL. Vieleicht könnt ihr Irgend etwas sehen
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;
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#5

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:37
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)));
}
  Mit Zitat antworten Zitat
Benutzerbild von Ultimator
Ultimator

Registriert seit: 17. Feb 2004
Ort: Coburg
1.860 Beiträge
 
FreePascal / Lazarus
 
#6

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:39
Bin ich blöd () oder ist
Delphi-Quellcode:
repeat
until (c = 0) or (i >= c);
eigentlich das genaue Gegenteil von
Code:
do
 while( (c == 0) || (i >= c) );
Schleife Eins wiederholt alles solange, bis c=0 ist, oder i >= c ist.
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?

Wenn ja, dann mach aus dem C++-Code besser
Code:
do
 while( (c!=0) || (i < c) );



//edit: *ARGH* Zu langsam. -.- Aber ausführlicher
Julian J. Pracht
  Mit Zitat antworten Zitat
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#7

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:49
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:
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)));
}
der Code funktioniert aber net
Code:
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)));
}
Warum muss ich negieren? ist doch eine Austrittsbedingnug? So ist es zumindestens in Delphi.

Mein Problem ist erstmal gelöst. THX.

Ich hoffe aber trotzdem auf Aufklärung, will ja nicht dumm sterben .
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#8

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 17:58
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?
Miniaturansicht angehängter Grafiken
fehlermeldung_255.png  
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
clues1

Registriert seit: 11. Feb 2004
97 Beiträge
 
#9

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 18:14
Schei.. Problem noch nicht gelösst. Mir ist gerade eben aufgefallen, dass nun die komplette Anwenung hängen bleibt, denke mal Unendlichkeitsschleife.
Meine Easy Database Komponenten[/url] (EDB) Datenbankfuntionen für Delphi Personal/Std und höher. MySQL, MSSQL, Access (JET), Oracle, CSV, TXT, DBase und noch viele mehr. http://www.delphipraxis.net/internal...ct.php?t=37505
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#10

Re: C++ und Delphi -> Rekursiv Problem

  Alt 18. Sep 2005, 18:18
Sorry, ich hab vergessen eine Zeile zu übersetzen Wenns ohne die Zeile funktioniert, liegts wohl an der einen ^^
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:15 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz