Stack Problem



  • Hallo,

    ich versuch mich eben an nem Stack-Template, bekomme aber paar Fehlermeldungen.
    Und ich stehe eben echt auf der Leitung, findet jemand nen Fehler?
    Bitte auch ein paar Anmerkungen zum Code, was verbessrungswürdig ist.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    typedef unsigned long ulong;
    
    template<class C> class Stack {
      struct Daten {
        C *dat;
        ulong anzahl;
        ulong maxanzahl;
      };
    
    public:
      Stack(ulong);
      ~Stack();
    
      int Push(C);
      C Pop();
      ulong Size();
    };
    
    template<class C> Stack<C>::Stack(ulong s)
    {
      Daten *foo;
      foo->Daten =  new(C[s]);
      if(foo->dat)
        {
          foo->anzahl = 0;
          foo->maxanzahl++;
        }
      else 
        {
          foo->maxanzahl = foo->anzahl = 0;
        }
    }
    
    template<class C> Stack<C>::~Stack()
    {
      if(foo->dat) delete[](dat);
    }
    
    template<class C> int Stack<C>::Push(C c)
    {
      if (anzahl<maxanzahl)
        {
          dat[anzahl++] = c;
          return 1;
        }
      else
        {
          return 0;
        }
    }
    
    template<class C> C Stack<C>::Pop()
    {
      if (anzahl>0)
        {
          return dat[--anzahl];
        }
      else
        {
          return 0;
        }
    }
    
    template<class C>
    ulong Stack<C>::Size()
    {
      return anzahl;
    }
    
    int main()
    {
      const ulong SIZE = 100;
      Stack<char> stack(SIZE);
    
      char str[SIZE];
      cout << "Bitte einen String eingeben: ";
      cin-getline(str,SIZE);
    
      for (int x = 0; x<strlen(str); ++x)
        stack.Push(str[x]);
        cout << '\n';
    
        while (stack.Size()) 
          cout << stack.Pop();
    }
    


  • glaubst du nicht, dass es klever wäre, wenn du die Fehler gleich mit posten würdest?



  • So gehts nu, bekomme jetzt aber nen Core Dump.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    typedef unsigned long ulong;
    
    template<class C> class Stack {
      struct Daten {
        C *dat;
        ulong anzahl;
        ulong maxanzahl;
      };
      Daten *foo;
    
    public:
      Stack(ulong);
      ~Stack();
    
      int Push(C);
      C Pop();
      ulong Size();
    };
    
    template<class C> Stack<C>::Stack(ulong s)
    {
      foo->dat =  new(C[s]);
      if (foo->dat)
        {
          foo->anzahl = 0;
          foo->maxanzahl++;
        }
      else 
        {
          foo->maxanzahl = foo->anzahl = 0;
        }
    }
    
    template<class C> Stack<C>::~Stack()
    {
      if (foo->dat) delete[](foo->dat);
    }
    
    template<class C> int Stack<C>::Push(C c)
    {
      if (foo->anzahl<foo->maxanzahl)
        {
          foo->dat[foo->anzahl++] = c;
          return 1;
        }
      else
        {
          return 0;
        }
    }
    
    template<class C> C Stack<C>::Pop()
    {
      if (foo->anzahl>0)
        {
          return foo->dat[foo->anzahl--];
        }
      else
        {
          return 0;
        }
    }
    
    template<class C>
    ulong Stack<C>::Size()
    {
      return foo->anzahl;
    }
    
    int main()
    {
      const ulong SIZE = 100;
      Stack<char> stack(SIZE);
    
      char str[SIZE];
      cout << "Bitte einen String eingeben: ";
      cin.getline(str,SIZE);
    
      for (int x = 0; x<strlen(str); ++x)
        stack.Push(str[x]);
        cout << "\n\n";
    
        while (stack.Size()) 
          cout << stack.Pop();
    }
    


  • In deinem Konstruktor wird der foo Pointer verwendet. Dieser ist aber nicht initialisiert worden, zeigt also irgendwo hin, daher kommt deine Speicherzugriffsverletzung.

    EDIT: dein Pop() sollte übrigens erst erniedrigen:

    return foo->dat[--foo->anzahl];
    

    Gruß
    Entyl Sa



  • Ok, ich habe hier ne neue, etwas übersichtlichere version,
    bekomme folgende fehler beim kompilieren:

    bash-2.05b$ g++ -o stack stack.cpp main.cpp
    stack.cpp: In method Stack<T>::Stack(long unsigned int)': stack.cpp:12: parse error before;'
    stack.cpp: In method int Stack<T>::Push(T)': stack.cpp:22: parse error before?'
    stack.cpp:22: parse error before `:'
    stack.cpp:23: confused by earlier errors, bailing out

    stack.hpp

    template<class T> class Stack {
    	T* data;
    	unsigned long anzahl;
    	unsigned long maxanzahl; 
    
    public:
    	Stack(unsigned long);
    	~Stack();
    
    	int Push(T);
    	T 	Pop();
    
    	unsigned long Size();
    };
    

    stack.cpp

    #include <iostream>
    #include "stack.hpp"
    
    using namespace std;
    
    template<class T> Stack<T>::Stack(unsigned long s)
    {
    	data = new(T[s]);
    
    	if(data)  
    
    	anzahl = 0 ? maxanzahl++; : anzahl = maxanzahl = 0;
    }
    
    template<class T> Stack<T>::~Stack()
    {
    	if (data) delete[](data);
    }
    
    template<class T> int Stack<T>::Push(T t)
    {
    	if (anzahl < maxanzahl) ? data[anzahl++] = c; return 1; : return 2;
    }
    
    template<class T> T Stack<T>::Pop()
    {
    	if (anzahl > 0) ? return data[--anzahl]; : return 1;
    }
    
    template<class T> unsigned long Stack<C>::Size()
    {
    	return anzahl;
    }
    

    main.cpp

    #include <iostream>
    #include "stack.hpp"
    
    using namespace std;
    
    int main()
    {
    }
    


  • hat sich erledigt, hab den fehler...ich blödmann



  • jo wieder n paar dumme fehler:

    bash-2.05b$ g++ -o stack stack.cpp main.cpp
    /var/tmp/cc2RdDnN.o: In function main': /var/tmp/cc2RdDnN.o(.text+0x17): undefined reference toStack<char>::Stack(unsigned long)'
    /var/tmp/cc2RdDnN.o(.text+0x32): undefined reference to Stack<char>::~Stack(void)' /var/tmp/cc2RdDnN.o(.text+0x4a): undefined reference toStack<char>::~Stack(void)'

    stack.hpp

    template<class T> class Stack {
    	T* data;
    	unsigned long anzahl;
    	unsigned long maxanzahl; 
    
    public:
    	Stack(unsigned long);
    	~Stack();
    
    	int Push(T);
    	T 	Pop();
    
    	unsigned long Size();
    };
    

    stack.cpp

    #include <iostream>
    #include "stack.hpp"
    
    using namespace std;
    
    template<class T> Stack<T>::Stack(unsigned long s)
    {
    	data = new(T[s]);
    
    	if(data)  
    	{
    	anzahl = 0; 
    	maxanzahl++;
    	}
    	else { 
    	anzahl = maxanzahl = 0;
    	}
    }
    
    template<class T> Stack<T>::~Stack()
    {
    	if (data) delete[](data);
    }
    
    template<class T> int Stack<T>::Push(T t)
    {
    	if (anzahl < maxanzahl) { data[anzahl++] = t; return 1; } 
    	else return 0;
    }
    
    template<class T> T Stack<T>::Pop()
    {
    	if (anzahl > 0) { return data[--anzahl];  }
    	else return 1;
    }
    
    template<class T> unsigned long Stack<T>::Size()
    {
    	return anzahl;
    }
    

    main.cpp

    #include <iostream>
    #include "stack.hpp"
    
    using namespace std;
    
    int main()
    {
    	const unsigned long SIZE = 100;
    	Stack<char> stack(SIZE);
    
    	char str[SIZE];
    	cout << "Bitte eine String eingeben: ";
    	cin.getline(str,SIZE);
    
    	for (int x = 0; x<strlen(str); ++x)
        stack.Push(str[x]);
        cout << "\n\n";
    
        while (stack.Size()) 
          cout << stack.Pop(); 
    }
    

Anmelden zum Antworten