我有一个C ++类classA,我通过operator new
动态创建自定义类classB的对象实例。
我已成功完成了数百次成功;但是在这种情况下,new运算符返回一个NULL指针,即使我已经在构造函数中检查了this
指针的值,并且它返回了一个非null
指针。
我创建了一个复制最小工作示例的类来重现故障。但是,在这种情况下,运算符new
按预期工作。
据我所知,这必定是因为内存分配失败。
有没有人有任何关于如何解决这个问题的建议,从哪里开始或者这个mihaviour可能是什么原因?
我很乐意发布我的整个代码,但我不想厌倦任何人...
我希望有人可以帮助我。
提前为你的时间做好准备!
更新:代码按要求 为了让你认识下面的方法,问题就出现了。变量_radice为NULL(赋值后未更改)。
template <class T>
void AlberoNArio<T>::inserisciRadice(tipoElemento elemento)
{
_radice==new NodoAlberoNArioLista<T>();
cout<<endl<<_radice<<endl;
}
NodoAlberoNArioLista.h
#ifndef _NODO_ALBERO_N_ARIO_LISTA_H
#define _NODO_ALBERO_N_ARIO_LISTA_H
template <class T>
class NodoAlberoNArioLista
{
public:
typedef T tipoElemento;
typedef NodoAlberoNArioLista<T>* posizione;
tipoElemento _elemento;
posizione _padre;
NodoAlberoNArioLista();
NodoAlberoNArioLista(tipoElemento, posizione);
NodoAlberoNArioLista(NodoAlberoNArioLista<T>&);
NodoAlberoNArioLista<T>& operator=(NodoAlberoNArioLista<T>&);
static const posizione POSIZIONENULLA;
};
template <class T>
const typename NodoAlberoNArioLista<T>::posizione NodoAlberoNArioLista<T>::POSIZIONENULLA=0;
template<class T>
NodoAlberoNArioLista<T>::NodoAlberoNArioLista()
{_padre=0; cout<<endl<<endl<<endl<<this<<endl<<endl<<endl;}
template<class T>
NodoAlberoNArioLista<T>::NodoAlberoNArioLista(tipoElemento elemento, posizione padre)//==NULL) da modificare accordingly **LEO**
{
_elemento=elemento;
_padre=padre;
cout<<endl<<endl<<endl<<this<<endl<<endl<<endl;
}
template<class T>
NodoAlberoNArioLista<T>::NodoAlberoNArioLista(NodoAlberoNArioLista<T>& nodo)
{
_elemento=nodo._elemento;
}
template<class T>
NodoAlberoNArioLista<T>& NodoAlberoNArioLista<T>::operator=(NodoAlberoNArioLista<T>& nodo)
{
_elemento=nodo._elemento;
}
#endif
AlberoNArioAstratto.h
#ifndef _ALBERO_N_ARIO_ASTRATTO_H
#define _ALBERO_N_ARIO_ASTRATTO_H
#include <iostream>
#include<sstream>
#include <string>
using std::cout;
using std::istream;
using std::ostream;
using std::endl;
using std::string;
using std::istringstream;
template <class T, class P>
class AlberoNArioAstratto
{
public:
typedef T tipoElemento;
typedef P posizione;
virtual bool vuoto() const = 0;
virtual posizione radice() const = 0;
virtual void inserisciRadice(tipoElemento) = 0;
};
const string INIZIOFIGLITOKEN="[";
const string FINEFIGLITOKEN="]";
template <class T, class P>
istream &operator>>(istream &is, AlberoNArioAstratto<T,P>& alberoNArio)
{
typename AlberoNArioAstratto<T,P>::posizione tempPosizioneNodoAlbero;
string rigaElemento;
typename AlberoNArioAstratto<T,P>::tipoElemento tempElemento;
getline(is, rigaElemento);
istringstream iStringStream(rigaElemento);
iStringStream >> tempElemento;
alberoNArio.inserisciRadice(tempElemento);
tempPosizioneNodoAlbero=alberoNArio.radice();
getline(is, rigaElemento);
return is;
}
template <class T, class P>
ostream &operator<<(ostream &os, const AlberoNArioAstratto<T,P>& alberoNArio)
{
typename AlberoNArioAstratto<T,P>::posizione _tempRadice;
typename AlberoNArioAstratto<T,P>::posizione tempPosizioneNodoAlbero;
typename AlberoNArioAstratto<T,P>::tipoElemento tempElemento;
if (alberoNArio.vuoto()==true)
{return os;}
_tempRadice=alberoNArio.radice();
os<<tempElemento<<endl;
return os;
}
#endif
AlberoNArio.h
#ifndef _ALBERO_N_ARIO_LISTA_FIGLI_H
#define _ALBERO_N_ARIO_LISTA_FIGLI_H
#include "AlberoNArioAstratto.h"
#include "NodoAlberoNArioLista.h"
template <class T>
class AlberoNArio:public AlberoNArioAstratto<T, NodoAlberoNArioLista<T>* >
{
public:
typedef typename AlberoNArioAstratto<T, NodoAlberoNArioLista<T>* >::tipoElemento tipoElemento;
typedef typename AlberoNArioAstratto<T, NodoAlberoNArioLista<T>* >::posizione posizione;
AlberoNArio();
AlberoNArio(const AlberoNArio&);
~AlberoNArio();
void crea();
bool vuoto() const;
void inserisciRadice(tipoElemento);
posizione radice() const;
private:
static const posizione POSIZIONENULLA;
posizione _radice;
};
template <class T>
const typename AlberoNArio<T>::posizione AlberoNArio<T>::POSIZIONENULLA=NodoAlberoNArioLista<T>::POSIZIONENULLA;
//costruttori
template <class T>
AlberoNArio<T>::AlberoNArio()
{
crea();
}
template <class T>
AlberoNArio<T>::AlberoNArio(const AlberoNArio<T>& alberoNArio)
{
}
//distruttore
template <class T>
AlberoNArio<T>::~AlberoNArio()
{
}
template <class T>
void AlberoNArio<T>::crea()
{ _radice=POSIZIONENULLA; }
template <class T>
bool AlberoNArio<T>::vuoto() const
{ return (_radice==POSIZIONENULLA); }
template <class T>
void AlberoNArio<T>::inserisciRadice(tipoElemento elemento)
{
_radice==new NodoAlberoNArioLista<T>();//elemento,POSIZIONENULLA);
cout<<endl<<_radice<<endl;
}
template <class T>
typename AlberoNArio<T>::posizione AlberoNArio<T>::radice() const
{
return _radice;
}
#endif
Main.cpp的
#include <cstdlib>
#include <iostream>
#include "AlberoNArio.h"
#include <fstream>
using namespace std;
typedef AlberoNArio<int> AlberoGenealogico;
typedef AlberoGenealogico::posizione posizione;
int main(int argc, char *argv[])
{
AlberoGenealogico alberoGenealogico;
string fileAlberoGenealogico="Integers.txt";
ifstream filestreamAlberoGenealogico;
filestreamAlberoGenealogico.open(fileAlberoGenealogico.c_str(),ios::in);
if(!filestreamAlberoGenealogico)
{
cout << "Impossibile aprire il file "<<fileAlberoGenealogico<<"."; //<< argv[1] << " for reading.\n";
return (EXIT_FAILURE);
}
filestreamAlberoGenealogico>> alberoGenealogico;
cout<<endl<<alberoGenealogico<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Interger.txt 1 2 2 3 4 五 56
答案 0 :(得分:4)
_radice==new NodoAlberoNArioLista<T>();
应该是
_radice=new NodoAlberoNArioLista<T>();