C++新运算符返回新的意外-Dev cpp
C++ new operator returning new unexpected -Dev-cpp
我有一个C++类classA,在那里我通过运算符new
动态创建自定义类classB的对象实例。
我已经成功地做了几百次了;然而,在这种情况下,新的运算符返回一个NULL指针,尽管我已经检查了构造函数中this
指针的值,并且它返回了一个非null
指针。
我创建了一个类,复制了一个复制故障的最小工作示例。然而,在这种情况下,运算符new
按预期工作。
据我所知,这一定是由于内存分配失败。
有人对如何解决这个问题有什么建议吗?从哪里开始?或者是什么原因导致了这个问题?
我很乐意发布我的全部代码,但我不想让任何人感到无聊。。。
我希望有人能帮助我。
提前为您争取时间!
UPDATE:按要求编码为了您的方便,请在问题发生的地方使用下面的方法。变量_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.txt1.2.2.3.4.5.56
_radice==new NodoAlberoNArioLista<T>();
应该是
_radice=new NodoAlberoNArioLista<T>();
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 链表中的线性搜索[语言:C++编译器:Dev Cpp 4.9.9.2]
- C++新运算符返回新的意外-Dev cpp
- Dev -Cpp编译器编译错误
- Dev-Cpp中读取文件数据失败
- 来自dev-cpp中文件的stdin
- wx dev Cpp GDI加上问题