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 */