Im пытается изменить размер моего стека, но моя программа продолжает заканчиваться после «cout». На выходном терминале отображается 1, после чего программа завершается. В этом случае T является int и размер по умолчанию равен 10. Любая помощь будет высоко ценится.Невозможно изменить размер шаблона типа
#include <iostream>
#include <fstream>
using namespace std;
template <typename T>
class stack {
public:
int topStack;
T* stack1;
int size;
void copy(const stack& other);
void move(stack&& other);
// constructor
stack();
// destructor
~stack()
{
delete[] stack1;
};
// copy constructor
stack (const stack&);
// copy assignment
stack& operator= (const stack&);
// move constructor
stack (stack&&);
// move assignment
stack& operator= (stack&&);
T& top() const; // return the top element
void pop(); // remove the top element
void push(const T&); // add element on top of stack
void push (T&&); // add element on top of stack
bool empty() const; // is the stack empty?
void clear(); // remove all elements
ostream& print(ostream&, stack&);
void resize();
};
//Default Constructor
template <typename T>
stack<T>::stack()
{
size=10;
stack1= new T[size];
for(int b =0; b < size; b ++)
{
stack1[b] = T();
}
topStack =-1;
}
//Copy Constructor
template <typename T>
void stack<T>::copy(const stack& other)
{
topStack = other.topStack;
stack1= new T[other.size];
size =other.size;
for(int i=0; i< other.size ; i++)
{
stack1[i]=other.stack1[i];
}
}
//Copy assignment
template <typename T>
stack<T>& stack<T>::operator =(const stack& other)
{
if (this == &other) return *this;
T* store = new T[other.size];
for(int g =0; g < other.size ; g++)
{
store[g]= other.stack1[g];
}
delete[] stack1;
this->stack1 = store;
this->size = other.size;
this-> topStack = other.topStack;
return *this;
}
//Move Constructor
template<typename T>
void stack<T>::move(stack && other)
{
topStack = other.topStack;
other.topStack = 0;
stack1 = other.stack1;
for(int u =0; u < other.size ; u++)
{
other.stack1[u]=0;
}
size = other.size;
other.size=0;
}
//Move assignment
template <typename T>
stack<T>& stack<T>::operator= (stack&& other)
{
this->size = other.size;
other.size=0;
this->topStack = other.topStack;
other.topStack=0;
this->stack1 = other.stack1;
for(int u =0; u < this->size ; u++)
{
other.stack1[u]=0;
}
return *this;
}
//Checks if stack is empty
template <typename T>
bool stack<T>::empty() const
{
return topStack == -1;
}
//Resize array
template<typename T>
void stack<T>::resize()
{
cout << "DAYYYY55UM";
T* storage = new T[this->size*2];
cout << "DAYYYYUM";
for(int r=0; r < this->size ; r++)
{
storage[r]= this->stack1[r];
}
delete[] this->stack1;
this->stack1= storage;
this->size = size*2;
cout << "DAYYYYUM";
}
//Returns top
template <typename T>
T& stack<T>::top() const
{
if(empty())
{
cout << "ERROR: Stack is empty. "<< endl;
return;
//Make a throw catch statement here
}
return stack1[topStack];
}
//Pop
template <typename T>
void stack<T>::pop()
{
if(empty())
{
cout << "ERROR: Stack is empty." << endl;
return;
}
stack1[topStack] =0;
topStack--;
}
//Push
template <typename T>
void stack<T>::push(const T& q)
{
if(topStack < size)
{
topStack++;
stack1[topStack] = q;
}else{
resize();
}
}
//Push
template <typename T>
void stack<T>::push(T&& q)
{
if(topStack < size)
{
topStack++;
stack1[topStack] = q;
}else{
resize();
}
}
//Print Function
template <typename T>
ostream& stack<T>::print(ostream& os, stack& other)
{
os << other.stack1 ;
}
template <typename T>
void stack<T>::clear()
{
for(int g=0; g < size; g++)
{
stack1[g]=0;
}
}
int main()
{
stack<int> world;
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
world.push(9);
world.push(40);
world.push(40);
cout << world.stack1[12] << endl;
return 0;
}
Что такое T? И размер? –
В этом случае T является значением int, а по умолчанию установлено значение 10 по умолчанию – Nicknack
и для следующего магического вопроса: что говорит отладчик? –