Nie wiedziałem jak do końca nazwać ten temat.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
class Stack{
public:
int *data; //tablica przechowująca elementy
int size; //rozmiar tablicy
int n; //aktualna liczba elementów na stosie
Stack(const Stack &s){
cout<<this<<endl;
}
Stack(int new_size){
this->size=new_size;
this->data = new int[size];
cout<<"Konstruktor 1-parametrowy: "<<this<<endl;
this->n=-1;
}
Stack(){
this->size=5;
this->data = new int[size];
cout<<"Konstruktor bezparametrowy: "<<cout<<this<<endl;
this->n=-1;
}
~Stack(){
delete this->data;
cout<<"Destruktor: "<<this<<endl;
}
void push(int value){
this->n++;
data[n]=value;
}
void pop(){
this->n--;
}
bool empty(){
if(n==-1)
return true;
else
return false;
}
bool full(){
if(n==size-1)
return true;
else
return false;
}
int top(){
return(data[n]);
}
};
// DODANE
void dodaj(Stack s, int a){
s.push(a);
}
int main(){
Stack s;
s.push(0);
dodaj(s, 1); // DODANE
dodaj(s, 2); // DODANE
while (!s.empty()) {
cout << s.top()<<" "<<s.n<<endl;
s.pop();
}
cout<<endl;
return 0;
}
W zadaniu trzeba dodać gotową funkcję dodaj() oraz 2-krotne wywołanie jej w programie głównym.
Treść:
Sprowokuj wystąpienie błędne działanie programu poprzez napisanie i wywołanie funkcji zewnętrznej.
Zastanów się, co powoduje błędne działanie programu i jak to naprawić.
Nie mam pojęcia o co dokładnie chodzi. Przy s.push(0) liczba zostaje dodana do stosu, jednak przy użyciu funkcji dodaj() program się zawiesza. Proszę o podanie jakiegoś tropu, nakierowania - może to coś z konstruktorem kopiującym?