неверное преобразование из int в C ++ (древовидная структура данных) - PullRequest
1 голос
/ 08 марта 2020

Я знаю, что есть и другие вопросы, подобные этому, но я не смог найти решение для моего. Я уверен, что это очень просто, я просто не очень хорошо понимаю указатели. Я пытаюсь реализовать структуру данных, вот код:

#include <iostream>
using namespace std;
//************************************************************************************
// GLROW CLASS
//************************************************************************************

template <class DT>
class GLRow; //class prototype

template <class DT>
ostream& operator <<(ostream& s, GLRow<DT>& oneGLRow);

template <class DT>
class GLRow {

        friend ostream& operator<< <DT>(ostream& s, GLRow<DT>& oneGLRow);

        protected:
                DT* _Info;
                int _Next;
                int _Down;

        public:
                GLRow ();
                GLRow (const DT& newInfo);
                GLRow (const GLRow<DT>& anotherOne);
                GLRow<DT>& operator= (const GLRow<DT>& anotherOne);             //Make sure you do $
                int getNext();
                int getDown();
                DT& getInfo() const;
                int setNext(int n);
                int setDown(int d);
                int setInfo (const DT& x);
                ~GLRow(); //destructor
};

//************************************************************************************
// ARRAYGLL CLASS
//************************************************************************************

template <class DT>
class ArrayGLL; //class prototype

template <class DT>
ostream& operator <<(ostream& s, ArrayGLL<DT>& oneGLL);

template <class DT>
class ArrayGLL {

        friend ostream& operator<< <DT>(ostream& s, ArrayGLL<DT>& OneGLL);

        protected:
                GLRow<DT>* myGLL;
                int maxSize;                                            //Maximum size of the array$
                int firstElement;
                int firstFree;

        public:

                ArrayGLL ();
                ArrayGLL (int size);
                ArrayGLL (ArrayGLL<DT>& anotherOne);
                ArrayGLL<DT>& operator= (ArrayGLL<DT>& anotherOne);
                void display ();                                        //display in parenthesis fo$
                int find (DT& key);                                     //return the index position$
                void findDisplayPath (DT& Key);                         // as you travel through th$
                int noFree ();                                          //return the number of free$
                int size ();                                            //return the number of elem$
                int parentPos(DT& Key);                                 // provide the location of $
                GLRow<DT>& operator [] (int pos);                       //return the GLRow that is $
                int getFirstFree();
                int getFirstElement();
                void setFirstFree (int pos);
                void setFirstElement (int pos);
                ~ArrayGLL ();                                           //destructor
};

//************************************************************************************
// GLROW CLASS DEFINITIONS
//************************************************************************************


//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
GLRow<DT>::GLRow()
{
        _Info = nullptr;
        _Next;
        _Down;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
GLRow<DT>::GLRow(const DT &newInfo)
{
         _Info = newInfo;
        _Next;
        _Down;
}

template <class DT>
GLRow<DT>::GLRow(const GLRow<DT> & anotherOne)
{
        _Info = anotherOne._Info;
        _Next = anotherOne._Next;
        _Down = anotherOne._Down;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
GLRow<DT> & GLRow<DT>::operator=(const GLRow<DT> &anotherOne)
{
        _Info = anotherOne._Info;
        _Next = anotherOne._Next;
        _Down = anotherOne._Down;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
int GLRow<DT>::getNext()
{
        return _Next;
}
//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
int GLRow<DT>::getDown()
{
        return _Down;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
DT & GLRow<DT>::getInfo() const
{
        return _Info;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
int GLRow<DT>::setNext(int n)
{
        _Next = n;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
int GLRow<DT>::setDown(int d)
{
        _Down =d;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
int GLRow<DT>::setInfo(const DT &x)
{
        _Info = x;
}

//------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------

template <class DT>
GLRow<DT>::~GLRow()
{
        _Next = -1;
        _Down = -1;
        delete _Info;
}


Я получаю сообщение об ошибке:

In file included from main.cpp:3:0:
tree.cpp: In instantiation of ‘GLRow<DT>::GLRow(const DT&) [with DT = int]’:
main.cpp:10:21:   required from here
tree.cpp:102:9: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
   _Info = newInfo;
         ^
make: *** [main.o] Error 1

Я просто не понимаю, почему это ошибка преобразования потому что _Info является указателем, и я передаю адрес newInfo в функцию? Я уверен, что это что-то супер просто, спасибо заранее!

1 Ответ

0 голосов
/ 08 марта 2020

В комментарии вы спросили:

Я думаю, я понимаю, но тогда как мне отформатировать его, если нет _info = newInfo?

Вы можете использовать

_info = new DT(newInfo);

Но тогда вам нужно убедиться, что вы правильно распределяете память динамически.

Лучшей альтернативой будет использование умный указатель вместо необработанного указателя.

std::unique_ptr<DT> _Info;

или

std::shared_ptr<DT> _Info;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...