This is weird, but every time I try to create and return an object in C++, Visual Studio returns me an error:
yourprogram.exe has triggered a breakpoint. occurred
This is my Stack class (Pila in italian, Nodo means Node, there is another class that handles every single node):
#ifndef PILAVT_H
#define PILAVT_H
#include <iostream>
#include "elemento.h"
using namespace std;
template <class Nodo>
class Pila
{
public:
typedef Nodo tipoelem;
Pila();
Pila(int);
~Pila();
void creaPila();
bool pilaVuota() const;
tipoelem leggiPila() const;
int dimensionePila() const;
void stampapila();
void fuoriPila();
void inPila(tipoelem);
private:
tipoelem* elementi;
int MAXLUNGH;
int testa;
};
template <class Nodo>
Pila<Nodo>::Pila()
{
elementi = new tipoelem[100]; // dimensione standard della pila
MAXLUNGH = 100;
creaPila();
}
template <class Nodo>
Pila<Nodo>::Pila(int N)
{
elementi = new tipoelem[N]; // dimensione N della pila
MAXLUNGH = N;
creaPila();
}
template <class Nodo>
Pila<Nodo>::~Pila()
{
delete[] elementi;
};
template <class Nodo>
int Pila<Nodo>::dimensionePila() const
{
return (testa);
}
template <class Nodo>
void Pila<Nodo>::creaPila()
{
testa = 0;
}
template <class Nodo>
bool Pila<Nodo>::pilaVuota() const
{
return testa == 0;
}
template <class Nodo>
Nodo Pila<Nodo>::leggiPila() const
{
return elementi[testa - 1];
}
template <class Nodo>
void Pila<Nodo>::fuoriPila()
{
if (!pilaVuota())
{
testa -= 1;
}
else
{
cout << "nessun elemento nella pila" << endl;
}
}
template <class Nodo>
void Pila<Nodo>::inPila(tipoelem el)
{
if (testa == MAXLUNGH)
{
cout << "raggiunta capacità massima della pila" << endl;
}
else
{
elementi[testa] = el;
testa++;
}
}
template <class Nodo>
void Pila<Nodo>::stampapila() {
while (pilaVuota()) {
std::cout << fuoriPila() << std::endl;
}
}
#endif // _PILAVT_H
Now, if I want to create a function that uses a stack and returns a stack with reverse elements, like this... :
template<class Nodo>
Pila<Nodo> reverse(Pila<Nodo>& S)
{
Pila<Nodo> P;
///
algorithm here, it's useless because Visual throws the error as soon as i declare a new object
///
return (P);
}
for completeness, this is my main:
#include <iostream>
#include <string>
#include "esame.h"
using namespace std;
int main()
{
Pila<int> P1;
P1.inPila(1);
cout << P1.leggiPila()<<" ";
P1.inPila(2);
cout << P1.leggiPila() << " ";
P1.inPila(2);
cout << P1.leggiPila() << " ";
P1.inPila(2);
cout << P1.leggiPila() << " ";
P1.inPila(3);
cout << P1.leggiPila() << " ";
P1.inPila(3);
cout << P1.leggiPila() << " ";
P1.inPila(4);
cout << P1.leggiPila() << " ";
P1.inPila(4);
cout << P1.leggiPila() << " ";
P1.inPila(4);
cout << P1.leggiPila() << " ";
P1.inPila(5);
cout << P1.leggiPila() << " ";
P1.inPila(5);
cout << P1.leggiPila() << " ";
cout << "" << endl;
cout <<"entro nella funzione remove "<< endl;
remove(P1, 4);
count(P1, 3);
reverse(P1);
return 0;
}
And this is my node class:
#ifndef _NODOPV_H
#define _NODOPV_H
#include <iostream>
using namespace std;
template<class T>
class Nodo
{
public:
Nodo();
Nodo(T);
~Nodo();
void setElemento(T);
T getElemento() const;
bool operator ==(Nodo&);
std::ostream& operator<<(const Nodo&);
private:
T elemento;
};
template<class T>
Nodo<T>::Nodo()
{}
template<class T>
Nodo<T>::~Nodo()
{}
template<class T>
Nodo<T>::Nodo(T label)
{
elemento = label;
}
template<class T>
void Nodo<T>::setElemento(T label)
{
elemento = label;
}
template<class T>
T Nodo<T>::getElemento() const
{
return elemento;
}
template<class T>
bool Nodo<T>::operator==(Nodo& n)
{
return (getElemento() == n.getElemento());
}
template<class T>
ostream& Nodo<T>::operator<<(const Nodo& nodo)
{
return cout << nodo.getElemento() << endl;;
}
# endif // _NODOPV_H
The program compiles really well, but as soon as i try to run the exe, an error occurs the moment main tries to call "reverse" function. I must say I always have problems when it comes to declaring objects outside main and outside classes. Never understood why. When full error loads, it pops up a new .cpp with this data:
HEAP[PILACORRETTA.exe]: Invalid address specified to RtlValidateHeap( 015B0000, 015C1538 )
PILACORRETTA.exe has triggered a breakpoint.
and this is the code of the cpp:
//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>
////////////////////////////////////////////////////////////////
// delete() Fallback Ordering
//
// +-------------+
// |delete_scalar<----+-----------------------+
// +--^----------+ | |
// | | |
// +--+---------+ +--+---------------+ +----+----------------+
// |delete_array| |delete_scalar_size| |delete_scalar_nothrow|
// +--^----^----+ +------------------+ +---------------------+
// | |
// | +-------------------+
// | |
// +--+--------------+ +------+-------------+
// |delete_array_size| |delete_array_nothrow|
// +-----------------+ +--------------------+
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK);
#else
free(block);
#endif
}
Now, what am I overlooking? Why there is an error EVERYTIME i try to declare an object in these exactly conditions?
thanks!
Aucun commentaire:
Enregistrer un commentaire