【问题标题】:invalid conversion from ‘int’ to ‘int*’ C++ (data tree structure)从“int”到“int*”C++(数据树结构)的无效转换
【发布时间】:2020-03-08 20:37:50
【问题描述】:

我知道还有很多类似的问题,但我一直无法找到我的解决方案。我敢肯定这很简单,我只是不太了解指针。我正在尝试实现数据树结构,代码如下:

#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 的地址传递给函数?我相信这非常简单,在此先感谢!

【问题讨论】:

  • 我确定有一个副本,但我在一分钟内找不到它。你没有传递一个指针。你正在传递一个参考。 C++ 中的&amp; 被重载来做几件事情,这就是,imo,对初学者来说非常困惑。 newInfo 是对 int 的 reference。这很可能在机器代码级别实现为指针传递,但 C++ 中的变量本身被视为非指针类型(即int
  • 我的理解是我会分配一个指向引用的指针。这就是为什么我不明白将_Info 分配给newInfo 的问题? _Info 是指针,newInfo 是引用?
  • 引用是语法糖(无耻地从上面的链接中窃取),它让我们通过 reference 传递值,而无需处理指针。它们提供了指针的好处,但充当了实际的数据类型。就好像您正在尝试执行 int a = 5 ; int* p_a = a; 那样(我希望显然)无法编译。
  • 我想我明白了,但是如果不是_info = newInfo,我应该如何格式化呢?

标签: c++ pointers tree


【解决方案1】:

在您提出的评论中,

我想我明白了,但是如果不是_info = newInfo,我应该如何格式化它?

你可以使用

_info = new DT(newInfo);

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

更好的选择是使用智能指针而不是原始指针。

std::unique_ptr<DT> _Info;

std::shared_ptr<DT> _Info;

【讨论】:

  • 不幸的是,需要使用原始指针,我无法更改该代码。请问新的 DT(newInfo) 是如何工作的?这是某种 inante 函数还是?
  • 是时候从en.cppreference.com/w/cpp/language/newgood textbook 了解动态内存分配/释放了。
猜你喜欢
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多