我知道还有其他类似问题,但是我还没有找到解决方案。我确信这很简单,只是我不太了解指针。我正在尝试实现数据街结构,这是代码:

#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的地址传递给函数?我敢肯定,这非常简单,谢谢!

最佳答案

在您询问的评论中,



你可以用

_info = new DT(newInfo);

但随后,您必须确保适当地管理动态分配的内存。

更好的选择是使用智能指针而不是原始指针。
std::unique_ptr<DT> _Info;

要么
std::shared_ptr<DT> _Info;

关于c++ - 从 ‘int’到 ‘int*’ C++的无效转换(数据树结构),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60591851/

10-11 02:22