I am having trouble with my copy constructor for a Deque program I am writing. It just does not seem to be doing any copying anyone notice a quick fix on this?

Deque::Deque(const Deque& oldDeque)
        {
        dArray = new int[oldDeque.dMaxSize];
        int sub = oldDeque.dMaxSize;
        for (int i = 0; i < oldDeque.dSize; i++)
                dArray[i] = oldDeque.dArray[sub];
        dMaxSize = oldDeque.dSize;
        }

the last line should be dMaxSize = oldDeque.dMaxSize; and you need to add dSize = oldDeque.dSize; Is the value of oldDeque.dSize > 0 ?

You can delete line 5 because that variable is not used.

This is what I came up with and now get a segmentation fault.

Deque::Deque(const Deque& oldDeque)
        {
        dArray = new int[oldDeque.dMaxSize];
        for (int i = 0; i < oldDeque.dSize; i++)
                dArray[i] = oldDeque.dArray[i];
        dMaxSize = oldDeque.dMaxSize;
        dSize = oldDeque.dSize;
        }

You will have to post the hold thing. The code you posted looks ok to me. Maybe the problem is somewhere else.

Alright I'll post the files I have created for ya. The assign4.cpp was written by my teacher what I have commented out is just to locate where the issue was.

The copy constructor failed to initialize values of dFront and dRear. The program crashes in the overloaded << operator because it uses dFront, which for that object contains some random huge value.

What do you mean by that? I thought they got initalized in the default constuctor?

Well the code I have for the << operator is what was giving to me in class as well. hmmm

What do you mean by that? I thought they got initalized in the default constuctor?

No. The default constructor does not initialize class variables at all. That's why you need to write your own construtors.

Well the code I have for the << operator is what was giving to me in class as well. hmmm

There is nothing wrong with the code in that function. The problem is in the constructor but showed its ugly head in the << operator code.

Oh alright, so the problem is def. in my copy constructor then right?

Still not having any luck and I been playing with it since I posted this. Any other suggestions would be great.

You apparently didn't make the changes I suggested

Deque::Deque(const Deque& oldDeque)
	{
	dArray = new int[oldDeque.dMaxSize];	
	for (int i = 0; i < oldDeque.dSize; i++)
		dArray[i] = oldDeque.dArray[i];
	dMaxSize = oldDeque.dMaxSize;
	dSize = oldDeque.dSize;
    dFront = oldDeque.dFront;
    dRear = oldDeque.dRear;
	}

	const Deque& Deque::operator=(const Deque& rightOp)
	{
	if(this != &rightOp)
    {
		delete[] dArray;
		dArray = new int[rightOp.dSize];
		for(int i = 0; i < rightOp.dSize; i++)
			dArray[i] = rightOp.dArray[i];
		dSize = rightOp.dSize;
        dFront = rightOp.dFront;
        dRear = rightOp.dRear; 
	}
	return *this;
	}

I went ahead and made the changes

Deque::Deque(const Deque& oldDeque)
        {
        dArray = new int[oldDeque.dMaxSize];
        for (int i = 0; i < oldDeque.dSize; i++)
                dArray[i] = oldDeque.dArray[i];
        dMaxSize = oldDeque.dMaxSize;
        dSize = oldDeque.dFront;
        dRear = oldDeque.dRear;
        }

        const Deque& Deque::operator=(const Deque& rightOp)
        {
        if(this != &rightOp)
                {
                delete[] dArray;
                dArray = new int[rightOp.dSize];
                for(int i = 0; i < rightOp.dSize; i++)
                        dArray[i] = rightOp.dArray[i];
                dSize = rightOp.dSize;
                dFront = rightOp.dFront;
                dRear = rightOp.dRear;
                }
        return *this;
        }

and my output came out incorrect

This is what I got:
deque 1 is empty
deque 1 (size 0):

deque 1 pushBack()
deque 1 is not empty
deque 1 (size 1): 17

Copy constructor
deque 1 (size 4): 17 2 6 4
deque 2 (size 0):

deque 1 (size 0):
deque 2 (size 0):

This is what I'm suppose to get:
deque 1 is empty
deque 1 (size 0):

deque 1 pushBack()
deque 1 is not empty
deque 1 (size 1): 17

Copy constructor
deque 1 (size 4): 17 2 6 4
deque 2 (size 4): 17 2 6 4

deque 1 (size 0):
deque 2 (size 4): 17 2 6 4

this is what I got

deque 1 is empty
deque 1 (size 0):

deque 1 pushBack()
deque 1 is not empty
deque 1 (size 1): 17

Copy constructor
deque 1 (size 4): 17 2 6 4
deque 2 (size 4): 17 2 6 4

deque 1 (size 0):
deque 2 (size 4): 17 2 6 4

Press any key to continue . . .

that is odd is that running it with all the code I had posted earlier in the topic?

Here is the complete file, just in case I changed something that I forgot to tell you about. Looks like I did change the constructor, but I don't see how that could have corrected anything other than when the parameter to the constructor is 0.

#include "Deque.h"
#include <iostream>
#include <iomanip>

using namespace std;

	Deque::Deque(int MaxSize)
	{
	dMaxSize = MaxSize;
	dArray = new int[dMaxSize];
    memset(dArray,0,dMaxSize * sizeof(int));
	dSize = 0;
	dFront = 0;
	dRear = dMaxSize - (MaxSize > 0);
	}

	Deque::~Deque()
	{
	delete[] dArray;
	}

	Deque::Deque(const Deque& oldDeque)
	{
	dArray = new int[oldDeque.dMaxSize];	
	for (int i = 0; i < oldDeque.dSize; i++)
		dArray[i] = oldDeque.dArray[i];
	dMaxSize = oldDeque.dMaxSize;
	dSize = oldDeque.dSize;
    dFront = oldDeque.dFront;
    dRear = oldDeque.dRear;
	}

	const Deque& Deque::operator=(const Deque& rightOp)
	{
	if(this != &rightOp)
    {
		delete[] dArray;
		dArray = new int[rightOp.dSize];
		for(int i = 0; i < rightOp.dSize; i++)
			dArray[i] = rightOp.dArray[i];
		dSize = rightOp.dSize;
        dFront = rightOp.dFront;
        dRear = rightOp.dRear;
	}
	return *this;
	}

	ostream& operator<<(ostream& osObject, const Deque& rightOp)
	{
	int sub = rightOp.dFront;
	for (int i = 0; i < rightOp.dSize; i++)
		{

		cout << rightOp.dArray[sub] << " ";
		sub = (sub+1) % rightOp.dMaxSize;
   		}
	return osObject;
	}
			
	void Deque::clear()
	{
	dSize = 0;
	dFront = 0;
	dRear = dMaxSize -1;
	}

	int Deque::size() const
	{
	return dSize;
	}

	bool Deque::empty() const	
	{
	if(dSize == 0)
		return true;
	else
		return false;
	}

	bool Deque::full() const
	{
	if(dSize == dMaxSize)
		return true;
	else
		return false;
	}

	int Deque::front() const
	{
	if(dSize == 0)
		{
		cout << "Error: Deque is empty.";
		return -1;
		}
	else
		return dFront;
	}

	int Deque::back() const
	{
	if(dSize==0)
		{
		cout << "Error: Deque is empty.";
		return -1;
		}
	else
		return dRear;
	}

	void Deque::pushFront(int item)
	{
	if(dSize == dMaxSize)
		cout << "Error: Deque is full.";
	else
		dFront--;
		if(dFront==0)
			dFront = dMaxSize - 1;
		dArray[dFront] = item;
		dSize++;
	}

	void Deque::pushBack(int item)
	{
	if(dSize == dMaxSize)
		cout << "Error: Deque is full.";
	else
		dRear++;
		if(dRear==dMaxSize)
			dRear = 0;
		dArray[dRear] = item;
		dSize++;
	}

	void Deque::popFront()
	{
	if(dSize==0)
		cout << "Error: Deque is empty." << endl;
	else
		dFront++;
		if(dFront==dMaxSize)
			dFront = dMaxSize - 1;
		dSize--;
	}

	void Deque::popBack()
	{
	if(dSize==0)
		cout << "Error: Deque is empty.";
	else
		dRear--;
		if(dRear==dMaxSize)
			dRear = 0;
		dSize--;
	}

everything looks the same to me, this makes no sense why this would happen, this is such a stupid thing to get hung up on

Here is what I got perhaps you'll catch something I did not.

#include "Deque.h"
#include <iostream>
#include <iomanip>

using namespace std;

	Deque::Deque(int MaxSize)
	{
	dMaxSize = MaxSize;
	dArray = new int[dMaxSize];
	dSize = 0;
	dFront = 0;
	dRear = dMaxSize - 1;
	}

	Deque::~Deque()
	{
	delete[] dArray;
	}

	Deque::Deque(const Deque& oldDeque)
	{
	dArray = new int[oldDeque.dMaxSize];
	for (int i = 0; i < oldDeque.dSize; i++)
		dArray[i] = oldDeque.dArray[i];
	dMaxSize = oldDeque.dMaxSize;
	dSize = oldDeque.dFront;
	dRear = oldDeque.dRear;
	}

	const Deque& Deque::operator=(const Deque& rightOp)
	{
	if(this != &rightOp)
		{
		delete[] dArray;
		dArray = new int[rightOp.dSize];
		for(int i = 0; i < rightOp.dSize; i++)
			dArray[i] = rightOp.dArray[i];
		dSize = rightOp.dSize;
		dFront = rightOp.dFront;
		dRear = rightOp.dRear;
		}
	return *this;
	}

	ostream& operator<<(ostream& osObject, const Deque& rightOp)
	{
	for (int i = 0, sub = rightOp.dFront; i < rightOp.dSize; i++)
		{
		osObject << rightOp.dArray[sub] << " ";
		sub = (sub+1) % rightOp.dMaxSize;
   		}
	return osObject;
	}
			
	void Deque::clear()
	{
	dSize = 0;
	dFront = 0;
	dRear = dMaxSize -1;
	}

	int Deque::size() const
	{
	return dSize;
	}

	bool Deque::empty() const	
	{
	if(dSize == 0)
		return true;
	else
		return false;
	}

	bool Deque::full() const
	{
	if(dSize == dMaxSize)
		return true;
	else
		return false;
	}

	int Deque::front() const
	{
	if(dSize == 0)
		{
		cout << "Error: Deque is empty.";
		return -1;
		}
	else
		return dFront;
	}

	int Deque::back() const
	{
	if(dSize==0)
		{
		cout << "Error: Deque is empty.";
		return -1;
		}
	else
		return dRear;
	}

	void Deque::pushFront(int item)
	{
	if(dSize == dMaxSize)
		cout << "Error: Deque is full.";
	else
		dFront--;
		if(dFront==0)
			dFront = dMaxSize - 1;
		dArray[dFront] = item;
		dSize++;
	}

	void Deque::pushBack(int item)
	{
	if(dSize == dMaxSize)
		cout << "Error: Deque is full.";
	else
		dRear++;
		if(dRear==dMaxSize)
			dRear = 0;
		dArray[dRear] = item;
		dSize++;
	}

	void Deque::popFront()
	{
	if(dSize==0)
		cout << "Error: Deque is empty." << endl;
	else
		dFront++;
		if(dFront==dMaxSize)
			dFront = 0;
		dSize--;
	}

	void Deque::popBack()
	{
	if(dSize==0)
		cout << "Error: Deque is empty.";
	else
		dRear--;
		if(dRear==dMaxSize)
			dRear = 0;
		dSize--;
	}

I didn't get the same result with your most recent post

deque 1 is empty
deque 1 (size 0):

deque 1 pushBack()
deque 1 is not empty
deque 1 (size 1): 17

Copy constructor
deque 1 (size 4): 17 2 6 4
deque 2 (size 0):

deque 1 (size 0):
deque 2 (size 0):

Press any key to continue . . .

ya that worked with ur code why do you think that is? i dont see what the difference would be?

Problem is in the constructor

Deque::Deque(const Deque& oldDeque)
	{
	dArray = new int[oldDeque.dMaxSize];
	for (int i = 0; i < oldDeque.dSize; i++)
		dArray[i] = oldDeque.dArray[i];
	dMaxSize = oldDeque.dMaxSize;
	dSize = oldDeque.dSize;
	dRear = oldDeque.dRear;
    dFront = oldDeque.dFront;
	}

Alright thanks alot got that working I have another error which I think if i get this one figured out the rest will be a breeze.

I'm getting this:
Test popFront() of empty deque
Error: Deque is empty.
deque 3 (size 3): 36 41 75

When I need this
Test popFront() of empty deque
Error - deque is empty
deque 3 (size 4): 36 41 75 28


Here is my pop front.

void Deque::popFront()
        {
        if(dSize==0)
                cout << "Error: Deque is empty." << endl;
        else
                dFront++;
                if(dFront==dMaxSize)
                        dFront = dMaxSize - 1;
                dSize--;
        }

you need { and } brackets around that else statement. As writen, on dFont++ is part of the else.

Alright I am about done with the program except this last part is giving me quite the nasty error, I think I located where it comes from. Which is my front() function.

int Deque::front() const
        {
        if(dSize == 0)
                {
                cout << "Error: Deque is empty." << endl;
                return -1;
                }
        else
                return dFront;
        }

int Deque::front() const

If the deque is empty, this method should print an error message and return -1. Otherwise, it should return the front item in the deque array.

Looks ok to me. What is that nasty error you are getting ?

I just copy and pasted this.

*** glibc detected *** assign4: munmap_chunk(): invalid pointer: 0x00000000022e00a0 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f031a52a948]
assign4[0x401ffa]
assign4[0x4019b4]
/lib/libc.so.6(__libc_start_main+0xe6)[0x7f031a4d51a6]
assign4(__gxx_personality_v0+0x49)[0x4009a9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 fd:00 508181 /home/turing/z136340/CS241/Assign4/assign4
00602000-00603000 rw-p 00002000 fd:00 508181 /home/turing/z136340/CS241/Assign4/assign4
022e0000-02301000 rw-p 022e0000 00:00 0 [heap]
7f031a4b7000-7f031a601000 r-xp 00000000 09:01 1140572 /lib/libc-2.7.so
7f031a601000-7f031a800000 ---p 0014a000 09:01 1140572 /lib/libc-2.7.so
7f031a800000-7f031a803000 r--p 00149000 09:01 1140572 /lib/libc-2.7.so
7f031a803000-7f031a805000 rw-p 0014c000 09:01 1140572 /lib/libc-2.7.so
7f031a805000-7f031a80a000 rw-p 7f031a805000 00:00 0
7f031a80a000-7f031a820000 r-xp 00000000 09:01 1140198 /lib/libgcc_s.so.1
7f031a820000-7f031aa20000 ---p 00016000 09:01 1140198 /lib/libgcc_s.so.1
7f031aa20000-7f031aa21000 rw-p 00016000 09:01 1140198 /lib/libgcc_s.so.1
7f031aa21000-7f031aaa3000 r-xp 00000000 09:01 1140571 /lib/libm-2.7.so
7f031aaa3000-7f031aca2000 ---p 00082000 09:01 1140571 /lib/libm-2.7.so
7f031aca2000-7f031aca4000 rw-p 00081000 09:01 1140571 /lib/libm-2.7.so
7f031aca4000-7f031ad95000 r-xp 00000000 09:01 228244 /usr/lib/libstdc++.so.6.0.10
7f031ad95000-7f031af94000 ---p 000f1000 09:01 228244 /usr/lib/libstdc++.so.6.0.10
7f031af94000-7f031af9a000 r--p 000f0000 09:01 228244 /usr/lib/libstdc++.so.6.0.10
7f031af9a000-7f031af9d000 rw-p 000f6000 09:01 228244 /usr/lib/libstdc++.so.6.0.10
7f031af9d000-7f031afb0000 rw-p 7f031af9d000 00:00 0
7f031afb0000-7f031afcc000 r-xp 00000000 09:01 1140577 /lib/ld-2.7.so
7f031b1bc000-7f031b1bf000 rw-p 7f031b1bc000 00:00 0
7f031b1c7000-7f031b1cb000 rw-p 7f031b1c7000 00:00 0
7f031b1cb000-7f031b1cd000 rw-p 0001b000 09:01 1140577 /lib/ld-2.7.so
7fff231b8000-7fff231cd000 rw-p 7ffffffea000 00:00 0 [stack]
7fff231ff000-7fff23200000 r-xp 7fff231ff000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted

I have no clue what all that means.

commented: a great help as usual +1

Haha ya thats what I'm saying its cool I'll go see my teacher monday or something about it. Thanks for all the help.

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.