Devo implementare uno stack usando un namespace Strutture dati, perņ ho errori di compilazione e non so come risolverli.
Questo č l' header:
Questo il cpp:codice:/* * Stack.h * * Created on: 15/nov/2011 * Author: Dario89 */ #ifndef STACK_H_ #define STACK_H_ namespace StruttureDati { class Stack { public: Stack(); public: ~Stack(); bool push(int a); bool pop(int &a); }; } /* namespace StruttureDati */ #endif /* STACK_H_ */
L' errore che ho č il fatto che dichiarando l' array non specifico la dimensione, io sono abituato in Java e una cosa come quella si puņ fare, in questo caso come dovrei correggere?codice:/* * Stack.cpp * * Created on: 15/nov/2011 * Author: Dario89 */ #include "Stack.h" #include <stdio.h> #include <iostream> using namespace std; namespace StruttureDati { int myStack[]; int size; #define CAPACITY 100 int top; Stack::Stack() { size=0; top=0; myStack=new int[CAPACITY]; // TODO Auto-generated constructor stub } bool push(int a) { if(size<CAPACITY){ myStack[size++]=a; top=size; return true; } printf("Error!!! Stack is full!"); return false; } bool pop(int &a) { if(size!=0) { myStack[a]=0; return true; } printf("Error!!! Stack is empty!"); return false; } int NumElements(){ return size; } Stack::~Stack() { //delete [] myStack; // TODO Auto-generated destructor stub } } /* namespace StruttureDati */ int main() { /* StruttreDati pila=new StruttureDati(); int number; cout << "Inserisci il numero di elementi da inserire nello stack" << endl; cin >> number;*/ }

Rispondi quotando