[英]invalid conversion from ‘int’ to ‘int*’ C++ (data tree structure)
我知道还有其他类似的问题,但我一直无法找到我的解决方案。 我确定这很简单,我只是不太了解指针。 我正在尝试实现一个数据树结构,代码如下:
#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 = newInfo
我应该如何格式化它?
你可以用
_info = new DT(newInfo);
但是,您必须确保适当地管理动态分配的内存。
更好的选择是使用智能指针而不是原始指针。
std::unique_ptr<DT> _Info;
或者
std::shared_ptr<DT> _Info;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.