gpt4 book ai didi

C++ 新运算符返回新的意外-Dev-cpp

转载 作者:行者123 更新时间:2023-11-30 01:57:57 25 4
gpt4 key购买 nike

我有一个 C++ 类 classA,我在其中通过运算符 new 动态创建自定义类 classB 的对象实例。

我已经成功地完成了数百次;然而在这种情况下,新运算符返回一个 NULL 指针,即使我已经在构造函数中检查了 this 指针的值并且它返回一个非 null 指针。

我已经创建了一个类来复制一个重现错误的最小工作示例。但是,在这种情况下,运算符 new 会按预期工作。

据我了解,这肯定是内存分配失败导致的。

有没有人对如何解决此问题、从哪里开始或导致此错误的原因有何建议?

我很乐意发布我的全部代码,但我不想让任何人感到厌烦...

我希望有人能帮助我。

提前感谢您的时间!

更新:要求的代码为了您的方便,下面的方法发生了问题。变量_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个 5个 56

最佳答案

_radice==new NodoAlberoNArioLista<T>();

应该是

_radice=new NodoAlberoNArioLista<T>();

关于C++ 新运算符返回新的意外-Dev-cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18023313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com