Hej i am getting the error:

error C2440: 'initializing' : cannot convert from 'int *' to 'GarbagePointer<T>'
1>          with
1>          [
1>              T=int
1>          ]
1>          Constructor for class 'GarbagePointer<T>' is declared 'explicit'
1>          with
1>          [
1>              T=int
1>          ]

--------------------------

But i really dont know how to either get around this or why it is even arising, it really should work accordingly to me..

it is being called like this:

int main(int argc, char** argv) {
   GarbagePointer<int> myGC=new int;
   *myGC = 10;
    return 0;
}


Can anyone help me, please ?

GarbagePointer code:

#ifndef GARBAGEPOINTER_H
#define	GARBAGEPOINTER_H
using namespace std;


template <typename T>
class GarbagePointer;

template <typename T>
class GarbagePointer {
public:

    explicit GarbagePointer(T* p = NULL) : value(p) { next=this;prev=this;
    };
//    GarbagePointer();
    GarbagePointer(const GarbagePointer& orig);
    virtual ~GarbagePointer();
    GarbagePointer<T> clone();

    T & operator =(const T* rhs);

    T * operator ->()const {
        return value;
    }

    T & operator *()const {
        return *value;
    }
private:
    GarbagePointer* next;
    GarbagePointer* prev;
    T* value;
};

template <typename T>
        T & GarbagePointer<T>::operator =(const T* rhs) {
    return clone();
}

//template <typename T>
//GarbagePointer<T> ::GarbagePointer() {
//
//}

template <typename T>
GarbagePointer<T>::GarbagePointer(const GarbagePointer& orig) {
    clone();
}

template <typename T>
GarbagePointer<T>::~GarbagePointer() {
    if ((this->next == this) && (this->prev == this)) {
        delete value; //eller array
        value = NULL;
    } else {
        value = NULL;
        prev->next = next;
        next->prev = prev;
    }
}

template <typename T>
GarbagePointer<T> GarbagePointer<T>::clone() {
    GarbagePointer<T> returnThis = new GarbagePointer<T> (this->value);
    returnThis.next = this->next;
    returnThis.prev = this;
    this->next.prev = returnThis;
    this->next = returnThis;
    return returnThis;
}

#endif	/* GARBAGEPOINTER_H */

Shouldn't this

GarbagePointer<int> myGC=new int;

be more like

GarbagePointer<int> myGC=new GarbagePointer<int>;

Shouldn't this

GarbagePointer<int> myGC=new int;

be more like

GarbagePointer<int> myGC=new GarbagePointer<int>;

Nope it really shouldnt, my garbagepointer should take a pointer of any sort into the template of which it was declared.

This compiles for me:

int* a = new int;
  GarbagePointer<int> myGC(a);

And it makes sense given this constructor:

explicit GarbagePointer(T* p = NULL) : value(p) { next=this;prev=this;
    };

This compiles for me:

int* a = new int;
  GarbagePointer<int> myGC(a);

And it makes sense given this constructor:

explicit GarbagePointer(T* p = NULL) : value(p) { next=this;prev=this;
    };

That seems to work... can you help me make the contructor so i can call it like i did before or you think it is impossible?

Well closer to what you had before would be a pointer gc object:

int* a = new int;
  GarbagePointer<int>* myGC = new GarbagePointer<int>(a);

but the syntax you had originally posted doesn't make sense - you can't instantiate a class of one type with an object of a different type!

GarbagePointer<int> myGC(new int);

is about the best solution...
Awesome that me and my teacher spent 3+ hours getting it to work, and i get answers in 20min online muhahaha stupid teach ;-P
thanks

Sure thing - glad it's working. Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.