Skip to main content
gamedev.net
🔒 Locked

Copy constructor problems (solved)

Started by arkmaster Dec 24, 2004 at 4:00 PM 4 replies 450+ views
Original Post
arkmaster
arkmaster
hey im using microsoft vc++ to compile this code...

template<class T>
class DL_List
{
public:
	DL_List();
	DL_List(const DL_List<T> &);
	~DL_List();
	...
private:
	long size;
	DL_List<T> *firstptr;
	DL_List<T> *lastptr;
	...
}

template<class T>
DL_List<T>::DL_List(const DL_List<T> &init)
{
//HERES A PROBLEM THAT IM BAFFLED AT...when i run the following 
//3 lines, no modifications are made to the current object
	size = 0;
	firstptr = NULL;
	lastptr = NULL;
	DL_ListNode<T>* currptr = init.firstptr;
	while (currptr)
	{
		T data = currptr->data;
		AddToBack(data);
		currptr = currptr->nextptr;
	}
}





the main problem im guessing is that it seems i can't modify the members of the 'this' pointer in the copy constructor. Does anyone know why this could be happening? if i need to post more code, let me know. thanks. [Edited by - arkmaster on December 24, 2004 10:08:37 PM]
Wavarian
Wavarian
How are you invoking the copy constructor?
arkmaster
arkmaster
I'm calling a function that uses call by value... ex:

DL_List list;
mean(list);

I do realize that I could avoid the problem by just modifying the function's definition to take a pointer, but then I won't know why I'm having problems...besides, I'll probably need the copy constructor for a more legit. reason later.
arkmaster
arkmaster
the thing that really confuses me is that when i run the debugger, it says 'size' value is something like 1258021. But when i attempt to modify it in the copy constructor, it executes the statement without any errors, but the value stays unchanged.
arkmaster
arkmaster
oh this may help with figuring out the problem...
//GetSize() is a public function of class DL_List<T> defined://long GetSize();template<class T>DL_List<T>::DL_List(const DL_List<T> &init){	long test_size = init.GetSize(); //ERROR COMPILING HERE	DL_ListNode<T>* currptr = init.firstptr;	while (currptr)	{		T data = currptr->data;		AddToBack(data);		currptr = currptr->nextptr;	}}

when compiling this, i get the following error message:

error C2662: 'GetSize' : cannot convert 'this' pointer from 'const class DL_List' to 'class DL_List &'


i suppose these two problems might be related to each other...and this current problem is a little less vague than my previous one.
Anyone know why this could be happening? Greatly appreciate the help.
arkmaster
arkmaster
ok never mind, i've solved my problem...apparently i needed to define some of my functions as const to work correctly
i.e long GetSize() const;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.