Forum: Instack + exception

Forum huvudsida -> Programmering -> Instack + exception

Sidor: 1

Till botten

Cirela 21:31 - 7:e Augusti 2005 | Post #1
Medlem
Inlägg: 18


Skicka PM
Hej

Detta är en enkel int-stack. Jag vill använda exception. Utan exception funkar bra men när jag inforgar
exception i mina två funktioner kommer följande:

....StringIndexOutOfRange......
undefined std....
...
...
...




Ser ni var felet ligger eller ska jag infoga en annan fil???hjälp mig


Mvh lisa

kOd:
// #################################### H-FIL

#ifndef MINSTACK_H
#define MINSTACK_H
#include <iostream.h>
#include <stdexcept>
using namespace std;

class Minstack{
    public:
Minstack(int storlek=10);
        ~Minstack(){delete[]stacken;}

void push(const int &obj)throw(length_error);
int pop()throw(length_error);
void print();

private:
    int *stacken;
int strlk,top;

Minstack(const Minstack *s){}
const Minstack operator=(const Minstack &s){return *this;};
};


Minstack::Minstack(int s)
:strlk(s)
{
    top=-1;
stacken = new int[strlk];
}

void Minstack::push(const int &obj)throw(length_error){
    if(top>=strlk-1)
    throw length_error("fel");
    stacken[++top]=obj;

}

int Minstack::pop()throw(length_error){
if(top<=-1)
    throw length_error("Fel");
return stacken[top--];
}
void Minstack::print(){
cout<<"Antal i stacken just nu: "<<(top+1)<<endl;
for(int i=top;i>=0;i--)
    cout<<stacken[i]<<" ";
}
#endif
// ############################# CPP-FIL ############################

#include <iostream.h>
#include "minstack.h"

void main(){
Minstack st(4);

try{

    st.push(8);
st.push(6);
    st.push(4);
st.push(2);

}
catch(length_error l){
    cout<<l.what();
}
st.print();
try{

    st.pop();
st.pop();
}
catch(length_error l){
    cout<<l.what();
}
st.print();


cin.get();
cin.get();
} Smiley

-------------------------
Ingen signatur!

Senast redigerad 21:33 - 7:e Augusti 2005


Independence 22:36 - 7:e Augusti 2005 | Post #2
Administratör
Inlägg: 1800


Skicka PM
Jag ändrade iostream.h till bara iostreom och satte returtypen i main-funktionen till int istället för void för att få programmet att gå igenom kompileringen men efter det var det inga problem. Jag fick inget error om StringIndexOutOfRange eller liknande. Använder Gcc 3.4.3 på Linux. Vet inte riktigt vad som är fel dock.

-------------------------

Vi är riddarna som säger fiskbulle!





Cirela 23:12 - 12:a Augusti 2005 | Post #3
Medlem
Inlägg: 18


Skicka PM
Jag använder inte linux utan borland.
Tack

-------------------------
Ingen signatur!



Celeron 07:30 - 13:e Augusti 2005 | Post #4
Medlem
Inlägg: 418


Skicka PM
Linux är ett operativsystem Smiley . Det här funkade i Dev-C++:

minstack.h:
  1.  
  2. #ifndef MINSTACK_H
  3. #define MINSTACK_H
  4. #include <iostream>
  5. #include <stdexcept>
  6. using namespace std;
  7.  
  8. class Minstack{
  9. public:
  10. Minstack(int storlek=10);
  11. ~Minstack(){delete[]stacken;}
  12.  
  13. void push(const int &obj)throw(length_error);
  14. int pop()throw(length_error);
  15. void print();
  16.  
  17. private:
  18. int *stacken;
  19. int strlk,top;
  20.  
  21. Minstack(const Minstack *s){}
  22. const Minstack operator=(const Minstack &s){return *this;};
  23. };
  24.  
  25.  
  26. Minstack::Minstack(int s)
  27. :strlk(s)
  28. {
  29. top=-1;
  30. stacken = new int[strlk];
  31. }
  32.  
  33. void Minstack::push(const int &obj)throw(length_error){
  34. if(top>=strlk-1)
  35. throw length_error("fel");
  36. stacken[++top]=obj;
  37.  
  38. }
  39.  
  40. int Minstack::pop()throw(length_error){
  41. if(top<=-1)
  42. throw length_error("Fel");
  43. return stacken[top--];
  44. }
  45. void Minstack::print(){
  46. cout<<"Antal i stacken just nu: "<<(top+1)<<endl;
  47. for(int i=top;i>=0;i--)
  48. cout<<stacken[i]<<" ";
  49. }
  50. #endif

.cpp filen:
  1.  
  2. #ifndef MINSTACK_H
  3. #define MINSTACK_H
  4. #include <iostream>
  5. #include <stdexcept>
  6. using namespace std;
  7.  
  8. class Minstack{
  9. public:
  10. Minstack(int storlek=10);
  11. ~Minstack(){delete[]stacken;}
  12.  
  13. void push(const int &obj)throw(length_error);
  14. int pop()throw(length_error);
  15. void print();
  16.  
  17. private:
  18. int *stacken;
  19. int strlk,top;
  20.  
  21. Minstack(const Minstack *s){}
  22. const Minstack operator=(const Minstack &s){return *this;};
  23. };
  24.  
  25.  
  26. Minstack::Minstack(int s)
  27. :strlk(s)
  28. {
  29. top=-1;
  30. stacken = new int[strlk];
  31. }
  32.  
  33. void Minstack::push(const int &obj)throw(length_error){
  34. if(top>=strlk-1)
  35. throw length_error("fel");
  36. stacken[++top]=obj;
  37.  
  38. }
  39.  
  40. int Minstack::pop()throw(length_error){
  41. if(top<=-1)
  42. throw length_error("Fel");
  43. return stacken[top--];
  44. }
  45. void Minstack::print(){
  46. cout<<"Antal i stacken just nu: "<<(top+1)<<endl;
  47. for(int i=top;i>=0;i--)
  48. cout<<stacken[i]<<" ";
  49. }
  50. #endif


PS. Använd inte iostream.h eftersom det inte är standard.

-------------------------
Det viktigaste är att ha roligt! Har ny hemsida: http://www.freewebs.com/cpperik/

Senast redigerad 07:31 - 13:e Augusti 2005


Sidor: 1

Forum huvudsida -> Programmering -> Instack + exception
Atom feed

Du får inte posta i den här tråden | Till toppen