Copy Constructor Issues for Stack<double>

Any ideas on why this copy constructor is not gonna work??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 #include "../include/dblstk.h"
#include <stack>

DoubleStack::DoubleStack(){
	stack_size=0;
	tos=0;
}

DoubleStack::DoubleStack(size_t capacity):stack_size(capacity) {}

DoubleStack::DoubleStack(const DoubleStack& rhs):stack_size(rhs.capacity()){
	delete this;
	if(rhs.getTOS()!=0){
		
		for (int i = 0 ; i < rhs.capacity();i++) {
			
			push(rhs.getData[i]);
		}
		
	}
};

size_t	DoubleStack::capacity() const {
	return stack_size;
}

size_t DoubleStack::getTOS() const {
	return tos;
}

DoubleStack::~DoubleStack(void){}

DoubleStack& DoubleStack::operator=(DoubleStack& rhs){
	if(this == rhs)
		return *this;
	else{
		delete this;
		return DoubleStack(rhs);
	}
}

int	DoubleStack::push(double& item) {
	if (stack_size < this->capacity()){
		data[tos]= item;
		tos++;
		stack_size++;
		return 1;
	}
	else return 0;
}

int	DoubleStack::pop(double& item) {
	if (tos > 0) {
		item = data[tos];
		tos--;
		stack_size--;
		return 1;
	} 
	else return 0;
}

int	DoubleStack::empty(void) {
	if (tos == 0){
		return 1;
	} else {
		return 0;
	}
}

size_t	DoubleStack::size(void) const{
	return stack_size;
}

int	DoubleStack::operator==(DoubleStack& rhs) {
	stack<double> mystack;
	for (int i = 0 ; i < mystack.size(); i++) {
			if (mystack[i] == rhs[i]) {
				return 1;
			} else {
				return 0;
			}
	}
}

double* DoubleStack::getData()const{
	return data;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 .h file

#include	<stdlib.h>
using namespace std;

class DoubleStack { 

	private:
	double	*data;		// Pointer to dynamic stack area.
	size_t	stack_size;	// Capacity of stack.
	size_t	tos;		// Top of stack. tos==0 ==> empty stack.

	public:
	DoubleStack();
		// Constructor.
	DoubleStack(size_t capacity);
	 
		// Copy Constructor.	
	DoubleStack(const DoubleStack& rhs);
	
		// Destructor.
	~DoubleStack(void);
	
		// Assignment operator=.		
	DoubleStack&	operator=(DoubleStack& rhs);
	
	size_t getTOS() const;
	
		// Add item to stack, making it the topmost item.
		// Return value 1=success/0=failure.
	int	push(double& item);

		// Remove top item from stack and return it in item.
		// Return value 1=success/0=failure.
	int	pop(double& item);

		// Determine if the stack is empty.
		// Return value 1=empty/0=non-empty.
	int	empty(void);
	
		// Inquire the capacity of the stack.
	size_t	capacity(void)  const;
	
		// Inquire the number of items on the stack.	
	size_t	size(void) const;

		// Compare 2 stacks to see if they contain the same data.
	int	operator==(DoubleStack& rhs);

	double* getData()const;
};


MY ERRORS::


In file included from /Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:9:
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/../include/dblstk.h:10:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
                ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:26:13: error: reference to non-static member function must be called; did you mean to call it with no arguments?
                        push(rhs.getData[i]);
                             ~~~~^~~~~~~
                                        ()
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:47:10: error: invalid operands to binary expression ('DoubleStack *' and 'DoubleStack')
        if(this == rhs)
           ~~~~ ^  ~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/utility:408:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:584:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'DoubleStack *'
operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:885:6: note: candidate template ignored: could not match 'istreambuf_iterator<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
     ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:988:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'DoubleStack *'
operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:1304:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'DoubleStack *'
operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1823:6: note: candidate template ignored: could not match 'allocator<type-parameter-0-0>' against 'DoubleStack *'
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
     ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2907:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2943:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2951:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack'
operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4758:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'DoubleStack *'
operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4807:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'DoubleStack *'
operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:4815:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'DoubleStack'
operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/deque:2825:1: note: candidate template ignored: could not match 'deque<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/stack:230:1: note: candidate template ignored: could not match 'stack<type-parameter-0-0, type-parameter-0-1>' against 'DoubleStack *'
operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:51:10: error: non-const lvalue reference to type 'DoubleStack' cannot bind to a temporary of type 'DoubleStack'
                return DoubleStack(rhs);
                       ^~~~~~~~~~~~~~~~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:90:15: error: type 'stack<double>' does not provide a subscript operator
                        if (mystack[i] == rhs[i]) {
                            ~~~~~~~^~
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:90:25: error: type 'DoubleStack' does not provide a subscript operator
                        if (mystack[i] == rhs[i]) {
                                          ~~~^~
1 warning and 5 errors generated.
[Finished in 0.2s with exit code 1] 
Don't declare using namespace std; in a header file. You probably shouldn't use it at all, but for homework it's ok I guess.

As for that copy constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DoubleStack::DoubleStack(const DoubleStack& rhs):stack_size(rhs.capacity()){
	delete this;
	if(rhs.getTOS()!=0){
		
		for (int i = 0 ; i < rhs.capacity();i++) {
			
			push(rhs.getData[i]);
		}
		
	}
};

class DoubleStack { 

	private:
	double	*data;		// Pointer to dynamic stack area.
	size_t	stack_size;	// Capacity of stack.
	size_t	tos;		// Top of stack. tos==0 ==> empty stack.
//... 

What does delete this mean?

The copy constructor should copy the values and duplicate that array. And that's another thing. I don't see an allocation for data anywhere in your code. And the destructor should release it, but it does nothing.
Last edited on
Why do I need allocation of Data? I have a getter for Data...
I need the using namespace std; because I don't want to write std:: every time I write int or anything else.

Can you explain why you think I need an allocation of Data?
data is defined as double* data

That means, data is a pointer to a double or to the start of an array of an array of doubles. So it's ok to treat is as an array, that's fine. The problem is you haven't created any memory for that array.

We'd expect it to be the size of stack_size, so you need to allocate an array of that size. That pointer must be made to point to something. As it stand, you have an uninitialised pointer. When you use your setter, it's overwriting some unspecified location in memory.

Alternatively, you can use an STL container, say a vector, to manage that allocation for you.

When you place something in a header file, it gets applied to any file that includes it. So specifying using namespace std; makes it apply to every source file that uses it. In your case, you're using that same declaration in the source file too, and that is causing the compiler message:
In file included from /Users/renathomas/Documents/online_courses
/C++Programming/Assignments/Assignment4/src/dblstk.cpp:9:
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/../include/dblstk.h:10:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
                ^
Last edited on
Topic archived. No new replies allowed.