Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Moin... mal wieder ein kleines c++ Anfängerproblem.

Ich habe mir eine Klasse StackAsFixedVector geschrieben.

Also einen Stack der auf einem nicht dynamischen Array basiert.

Hier die Implementierung der Klasse StackAsFixedVector.

Der ganze Spass steht in der Datei "J_StackAsFixedVector":


//   int read_top          Oberstes Element lesen

//   void clear_top        Oberstes Element entfernen

//   void write_top(wert)  Ein Element obendrauflegen

//   void clear            Stack leeren

//   bool empty            Ist der Stack leer?



class StackAsFixedVector

{

private:

   int data[5];

   int stack_size;

public:

   StackAsFixedVector() //Konstruktor

   {

   int stack_size = -1;

   }

   int read_top()//Oberstes Element lesen

   {

   return data[stack_size];

   }

   void clear_top()//Oberstes Element entfernen

   {

   stack_size = stack_size-1;

   }

   void write_top(int wert)//Ein Element obendrauflegen

   {

   stack_size = stack_size + 1;

   data[stack_size] = wert;

   }

   void clear()//Stack leeren      

   {

   stack_size = -1;

   }

   bool empty()//Ist der Stack leer?      

   {

   return stack_size == -1;

   }   

};

und nun hier die Implementierung der main.cpp:

#include <iostream>

#include "J_StackAsFixedVector.h"

using namespace std;


int main()

{

StackAsFixedVector stack; //Stack erzeugen


cout<<"Zahl eingeben: "; int zahl; cin>>zahl;

stack.write_top(zahl);



cin.get();

cin.get();

}

Das Programm wird zwar kompiliert, bricht aber mit der folgenden Fehlermeldung ab nachdem die Zahl eingegeben wurde und stack.write_top(zahl) aufgerufen wird:

"Unbenannt3.exe hat ein Problem festgestellt und muss beendet werden."

Kann mich irgendjemand auf den Fehler im Code aufmerksam machen den ich gemacht haben muss?

mfg

Jonsc1

Logisch... soll ja auch ein Konstruktor sein...

Also einfach aus "int stack_size = -1" ein "stack_size = -1" gemacht und schon läufts... und wegen so was hab ich mir ne Stunde lang den Kopf zerbrochen... :)

Danke dir!

Mit einer Intialisierungsliste wäre das nicht passiert:

   StackAsFixedVector() : stack_size(-1)

   {

   }

So weit bin ich noch nicht :)

Noch nicht gelernt...

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.