3 Replies - 116 Views - Last Post: 06 February 2012 - 09:25 PM Rate Topic: -----

Topic Sponsor:

#1 adolf625  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 22-November 08

Copy Constructor will not work

Posted 06 February 2012 - 07:38 PM

I have tried several ways to get this copy constructor to work, but everytime it either gives me an error prints everything out, except the values that were copied over, I guess I am going about setting up the constructor wrong, does anyone have any idea as to what I'm doing wrong here, thanks.

#include <iostream>
#include "List.h"
using namespace std;

List::List(){
    used = 0;
    currentPosition = 0;

    /* zeros out the array*/
    clearOutArray();
}

List::List(const List &toCopy){
    used = toCopy.used;
    for(int i = 0; i < used; i++){
        setPos(i + 1);
        listArray[i] = toCopy.listArray[i];
    }
}

void List::front(){
    currentPosition = 0;
}

void List::end(){
    if(used != 0){
        currentPosition = used - 1;
    }
}

void List::prev(){
    if(used > 0){
        currentPosition--;
    }
}

void List::next(){
    if((currentPosition + 1) < used){
        currentPosition++;
    }
}

int List::getPos(){
    return currentPosition;
}

void List::setPos(int toSet){
    if(toSet >= 0 && toSet <= used){
        currentPosition = toSet;
    }
}

List::ElementType List::getElement(){
    return listArray[currentPosition];
}

void List::erase(){
    for(int i = currentPosition + 1; i < used; i ++){
        listArray[i] = listArray[i+1];
    }
    used--;
}

int List::size(){
    return used;
}

bool List::empty(){
  return(used = 0);
}

void List::replace(int toReplace){
    if(!empty()){
        listArray[currentPosition] = toReplace;
    }
}

void List::clear(){
    clearOutArray();
}

void List::insertAfter(List::ElementType value){
    if(used == 0){
        listArray[0] = value;
    }
    else{
        for(int i = used; i > currentPosition; i--){
            listArray[used] = listArray[i -1];
        }
        listArray[currentPosition + 1] = value;
    }
    used++;
    currentPosition++;
}

void List::insertBefore(List::ElementType value){
   // if((currentPosition - 1) < 0){
        //cout << "The list is full";
    //}
    //else{
        if(used == 0){
            listArray[0] = value;
        }
        else{
            for(int i = used; i > currentPosition - 1; i--){
                listArray[used] = listArray[i-1];
            }
            listArray[currentPosition - 1] = value;
        }
        currentPosition++;
        used++;
   // }
}

void List::reverse(){
	for(int i = 0; i < used; i++){
		ElementType temp = listArray[i];
		listArray[i] = listArray[used - i];
		listArray[used - i] = listArray[i];
	}
}

void List::swap(List toSwap){
	for(int i = 0; i < used; i++){
		ElementType temp = listArray[i];
		listArray[i] = toSwap.listArray[i];
		toSwap.listArray[i] = listArray[i];
	}
}

int List::getUsed(){
	return used;
}

List &List::operator=(const List list1){
    for(int i = 0; i < used; i++){
        listArray[i] = list1.listArray[i];
    }
    return *this;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Copy Constructor will not work

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: Copy Constructor will not work

Posted 06 February 2012 - 09:08 PM

Please show your List definition.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 adolf625  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 22-November 08

Re: Copy Constructor will not work

Posted 06 February 2012 - 09:12 PM

View Postjimblumberg, on 06 February 2012 - 09:08 PM, said:

Please show your List definition.

Jim


#include <iostream>
using namespace std;

class List{
public:
    typedef unsigned int ElementType;
    static const ElementType CAPACITY = 20;

    /*Constructors*/
    List();
    List(const List &toCopy);

    /*Modifiers*/
    void front();
    void end();
    void prev();
    void next();
    int getPos();
    void setPos(int toSet);
	int getUsed();
    ElementType getElement();
    void erase();
    int size();
    bool empty();
    void replace(int toReplace);
    void clear();
    void insertAfter(ElementType value);
    void insertBefore(ElementType value);
	void reverse();
	void swap(List toSwap);

	/*Oveloaded methods*/
	friend ostream &operator<<(ostream &out, List toPrint);
	List &operator=(const List list1);

private:
    int used;   // Used to determine size and next available position in array
    int currentPosition;    // used for modifying and shuffling array values
    ElementType listArray[CAPACITY];    // Creates an array of size CAPACITY
    void clearOutArray(){
        for(int i = 0; i < CAPACITY; i++){
            listArray[i] = 0;
        }
    }
};



and my main file

#include <iostream>
#include "List.h"
using namespace std;

ostream &operator<<(ostream &out, List toPrint){
	for(int i = 0; i < toPrint.used; i++){
		toPrint.setPos(i);
		cout << toPrint.getElement() << " ";
	}
	return out;
}

bool operator==(List list1, List list2){
    return(list1.getUsed() == list2.getUsed());
}



int main()
  {
  	List a,b;  int endit;

	for (int i=1;i<=20;i++)
	   a.insertAfter(i);
	cout << "List a : " << endl;
    cout << "  "  << a << endl;
	cout << "Number of elements in a - " << a.size() << endl;

	for (int i=1;i<=20;i++)
	   b.insertBefore(i);
	cout << "List b : " << endl;
    cout << "  "  <<  b << endl;
	cout << "Number of elements in b - " << b.size() << endl;

	if ( a == b )
	    cout << "List a & b are equal" << endl;
	  else
	    cout << "List a & b are Not equal" << endl;

	a.front();
	b.front();
	cout << "First elmenet in list a & b: " << a.getElement() << ", "
				       << b.getElement() << endl;
	for ( int i = 0; i < a.size(); a.next(),i++);
	for ( int i = 0; i < b.size(); b.next(),i++);
	cout << "Last elmenet in list a & b: " << a.getElement() << ", "
				     << b.getElement() << endl;
	cout << endl << endl << " Start of new stuff" << endl;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	a.front();
	b.front();
	endit = a.size()/2;
	for ( int i = 1; i<endit; i++)
	{  a.next();
	   b.next();
	}

	cout << "New position in Obj 'a' position = " << a.size()/2 << endl;

	for ( int i=1; i<8; i++)
	{
	   a.erase();
	   b.replace(i);
	}

	cout << "Modified Object 'a' " << endl;
    cout << "List a: " << a << endl;

	List c(B);
	cout << "Object 'b' assigned to Object List c(B)" << endl;
    cout << "List b : " << b << endl;
    cout << "List c : " << c << endl;

	if ( c == b )
	    cout << "List c & b are equal" << endl;
	  else
	    cout << "List c & b are Not equal" << endl;


	List e;
	e = c;
	cout << "Object 'c' assigned to Object 'e':" << endl;
    cout << "List c : " << c << endl;
    cout << "List e : " << e << endl;

    List d;
	d=a;
	cout << "List 'd' assign list 'a' : " << endl;
	cout << "List a: " << a << endl;
	cout << "List d: " << d << endl;
	d.front();
	endit = d.size()/2;
	for (int i=1; i<=endit; i++)
	{
		d.next();
		d.erase();
	}
	cout << "Results after some erases: Object d  " << endl;
    cout << "List d : " << d << endl;

	d.front();
	for ( int i = 1; i < d.size(); d.next(), i++)
	{
		d.insertBefore(d.getElement()*2);
		d.next();
	}
	cout << "Results after some Replaces on d " << endl;
    cout << "List d : " << d << endl;

	a.front();
	for ( int i = 1; i < a.size(); a.next(), i++)
	{
		a.insertBefore(a.size()+a.getElement());
		a.next();
		a.erase();
	}
	cout << "Results after some weird stuff on list a" << endl;
    cout << "List a : " << a << endl;

    List alist(B);
    alist.clear();
    for (int i=1;i<=20;i++)
	   alist.insertAfter(i);
	cout << "New List alist : " << endl;
    cout << "  "  << alist << endl;
    alist.reverse();
    cout << "Reverse New List alist : " << endl;
    cout << "  "  << alist << endl;

    List newa;
    for (int i=1;i<=20;i++)
	   newa.insertAfter(i*3);
    cout << "List alist and newa before swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;
    alist.swap(newa);
    cout << "List alist and newa after swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;


	cout << endl << "  check out boundary condictions" << endl;
	List sq;
	cout << "number of elements in sq = " << sq.size() << endl;
	sq.front();
	sq.erase();
	cout << "sq values " << sq << endl;
	sq.insertBefore(1000);
	cout << "sq values " << sq << endl;
	sq.next(); sq.next();
	cout << "sq.getElement() = " << sq.getElement() << endl;
	cout << "sq values " << sq << endl;
	//system("pause");   //This statement not needed in code blocks, just VS.
	return 0;
  }




Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Programmer
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,084
  • Joined: 19-February 09

Re: Copy Constructor will not work

Posted 06 February 2012 - 09:25 PM

Hi, your copy constructor seems fine apart from

       setPos(i + 1);



which is not needed and not affecting the program.


In your copy assignment operator you neglect to update the used variable.

List &List::operator=(const List list1){
    for(int i = 0; i < used; i++){
        listArray[i] = list1.listArray[i];
    }
    return *this;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1