C++;新操作员返回新的意外-开发人员cpp 我有一个C++类类,在这里我通过操作符新< /C> >动态创建自定义类Casb的对象实例。
我已经成功地做了几百次了;然而,在这种情况下,新操作符返回一个空指针,即使我在构造函数中检查了C++;新操作员返回新的意外-开发人员cpp 我有一个C++类类,在这里我通过操作符新< /C> >动态创建自定义类Casb的对象实例。,c++,pointers,null,new-operator,C++,Pointers,Null,New Operator,我已经成功地做了几百次了;然而,在这种情况下,新操作符返回一个空指针,即使我在构造函数中检查了this指针的值,它返回一个非NULL指针 我创建了一个类,它复制了一个最小的工作示例,重现了错误。但是,在这种情况下,操作员new按预期工作 据我所知,这一定是由于内存分配失败 有没有人对如何解决这个问题,从哪里开始,或者这种行为的原因有什么建议 我很乐意发布我的全部代码,但我不想让任何人厌烦 我希望有人能帮助我 提前为你的时间做准备 更新:按要求编码 为了方便您,请在问题发生的地方使用下面的方法。变
this
指针的值,它返回一个非NULL
指针
我创建了一个类,它复制了一个最小的工作示例,重现了错误。但是,在这种情况下,操作员new
按预期工作
据我所知,这一定是由于内存分配失败
有没有人对如何解决这个问题,从哪里开始,或者这种行为的原因有什么建议
我很乐意发布我的全部代码,但我不想让任何人厌烦
我希望有人能帮助我
提前为你的时间做准备
更新:按要求编码
为了方便您,请在问题发生的地方使用下面的方法。变量_radice为空(赋值后不变)
模板
void AlberoNArio::inserisciRadice(tipoElemento elemento)
{
_radice==新节点AlberonarioLista();
cout\u radice==new NodoAlberoNArioLista();
应该是
\u radice=new NodoAlberoNArioLista();
\u radice==new NodoAlberoNArioLista();
应该是
\u radice=new NodoAlberoNArioLista()
发布执行此操作的代码将有所帮助如果您的最小工作示例再现了故障,它如何能够按预期工作?好的,那么,我将在几秒钟后发布。mihaviour?这是针对不当行为的新收缩吗?您理解的是错误的。非放置新建
从不返回空指针(除非程序有未定义的行为,或者其他地方有bug)。发布执行此操作的代码将有所帮助。如果您的最小工作示例再现了故障,它将如何按预期工作?好的,我将在几秒钟后发布。mihaviour?这是针对不当行为的新收缩吗?您理解的是错误的。非放置new
从不返回空指针(除非程序有未定义的行为,或者其他地方有bug)。我确信这解决了问题。谢谢。我非常习惯于编写这种类型的代码,我完全监督了它。谢谢!moverover,我被一段==合法的代码弄糊涂了。我确信这解决了问题。谢谢。我非常习惯于编写这种类型的代码,我完全监督了它。谢谢!moverover,我得到了确认与==合法的代码段一起使用。
template <class T>
void AlberoNArio<T>::inserisciRadice(tipoElemento elemento)
{
_radice==new NodoAlberoNArioLista<T>();
cout<<endl<<_radice<<endl;
}
#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
#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
#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
#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;
}