1

I know there are tones of other questions like this, but I haven't been able to find the solution to mine. I'm sure it's very simple I just don't understand pointers very well. I'm trying to implement a data stree structure here's the code:

#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;
}


I'm getting the error:

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

I just don't understand why it's a conversion error because _Info is a pointer and I'm passing the address of newInfo into the function? I'm sure it's something super easy, thanks in advance!

Gilbert22
  • 47
  • 6
  • 1
    I'm sure there's a duplicate, but I couldn't find it in under a minute. You're not passing a pointer. You're passing a reference. The `&` in C++ is overloaded to do several things, which is, imo, something very confusing to beginners. `newInfo` is a *reference* to an int. This is likely implemented at machine code level as pointer passing, but the variable itself in C++ is treated as the non-pointer type (ie, an `int`) – JohnFilleau Mar 08 '20 at 20:41
  • Here's a good 'duplicate' target: [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/q/57483/10871073). – Adrian Mole Mar 08 '20 at 20:43
  • It's my understanding that I would assign a pointer to a reference. That's why I don't understand the problem with assigning _Info to newInfo? _Info is a pointer and newInfo is a reference? – Gilbert22 Mar 08 '20 at 20:44
  • 1
    References are syntactic sugar (shamelessly stolen from the link above) that let us pass values by, well, *reference* without having to deal with pointers. They give the benefits of pointers, but act as the actual data type. It's as if you're trying to do `int a = 5 ; int* p_a = a;` which would (I hope obviously) fail to compile. – JohnFilleau Mar 08 '20 at 20:49
  • I think I understand, but then how should I format it if not _info = newInfo? – Gilbert22 Mar 08 '20 at 20:54

1 Answers1

0

In a comment you asked,

I think I understand, but then how should I format it if not _info = newInfo?

You could use

_info = new DT(newInfo);

But then, you have to make sure that you manage dynamically allocated memory appropriately.

A better alternative would be to use a smart pointer instead of a raw pointer.

std::unique_ptr<DT> _Info;

or

std::shared_ptr<DT> _Info;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Unfortunately, the use of a raw pointer is required, I can't change that code. May I ask how the new DT(newInfo) works? Is that some sort of inante function or? – Gilbert22 Mar 08 '20 at 21:03
  • It's time to learn about dynamic memory allocation/deallocation from https://en.cppreference.com/w/cpp/language/new or a [good textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Mar 08 '20 at 21:06