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

C++ new operator returning new unexpected -Dev-cpp

本文关键字:-Dev cpp 意外 返回 运算符 C++      更新时间:2023-10-16

我有一个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>();