Subscript Operators - HELP!

Write your question here.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  //my .cpp file

#include "../include/dblstk.h"
#include <iostream>
#include <stack>

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

DoubleStack::DoubleStack(size_t capacity) {

	data = new double[capacity];
	stack_size = capacity;
	tos = 0;
}

DoubleStack::DoubleStack(const DoubleStack& rhs):stack_size(rhs.capacity()){
	delete this;
	if (rhs.getTOS()!=0) {
		//DoubleStack *temp = new DoubleStack(rhs.capacity());
		//double popedTemp;
		for (int i = 0; i < rhs.capacity(); i++) {
			//rhs.pop(popedTemp);
			push(rhs.getData()[i]);
		}
		// for (int i = 0; i < stack_size; ++i) {
		// 	temp->pop(popedTemp);
		// 	rhs.push(popedTemp);
		// 	push(popedTemp);
		// }
	}
}

DoubleStack::~DoubleStack(void){}

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

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

DoubleStack& DoubleStack::operator =(DoubleStack& rhs){
	if (*this == rhs)
		return *this;
	else{
		delete this;
		return 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.getData()[i]== rhs.getData()[i]) {
				return 1;
			} else {
				return 0;
			}
	}
}

void DoubleStack::displayStack() {
	for (int i = 0; i < this->size(); i++) {
    	double x = data[i];
    	cout << x << endl;
	}
}

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
53
54
55
//my .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;

		void displayStack(void);
};


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:96:16: error: no member named 'getData' in 'std::__1::stack<double, std::__1::deque<double, std::__1::allocator<double> > >'
                        if (mystack.getData()[i]== rhs.getData()[i]) {
                            ~~~~~~~ ^
1 warning and 1 error generated.
[Finished in 0.3s with exit code 1]

stack<double> mystack;

This is an object of type stack. It has no function named getData, as you can see here : http://www.cplusplus.com/reference/stack/stack/

Why have you created an object of type stack there?
You wrote, using namespace std in your header file. The problem here is that no actual namespace named std is defined, since you included your custom header file BEFORE your iostream declaration. Therefore, you are getting your first error related to using namespace std. Either include iostream in your header as well, or include your custom header after iostream.
Or, never have using namespace std; The time has come to break this bad habit :+)

Just put std:: before each std thing, that is what all the expert coders do.
Or, like I prefer to do:

1
2
3
4
5
using std::cout;
using std::cin;
using std::string;
using std::endl;
and so on...


I include these 4 statements at the start of every iostream library program!
Or, like I prefer to do:


The trouble is that maintaining a using declaration list becomes tiresome as well, it's not hard to accumulate quite a few of them, especially when using algorithms. So in the end it's easier to just put std:: before each std thing.

Also std:: is explicit : for example std::copy versus boost::copy or some other copy from a different library.

And one should be putting their own code into it's own namespaces, so one should be used to qualifying everything with a namespace alias.

BTW, it's not recommended to use std::endl , it is a little inefficient because it flushes the buffer each time. Prefer to use '\n' instead.

That was just an example, and what I'm saying is only do those statements, depending on how many times you are going to use each. If you are using them once or twice, then add the std:: prefix. See, for I/O programs, for example, cout is used a lot, so using std::cout; is what i use.

I know it's bad with std::endl; But the thing is, even when buffering, couldn't you cout.flush() for that?
That was just an example ......


I used to do that too, now I just put std:: , because I found it better, presumably lots of others have come to the same conclusion.

I know it's bad with std::endl; But the thing is, even when buffering, couldn't you cout.flush() for that?


endl flushes the cout buffer every time: there is no need, cout will flush automatically when needed. Again, one doesn't usually see the expert coders using std::endl. One could of course still use std::flush or std::endl or std::unitbuf when required, as explained here:

http://en.cppreference.com/w/cpp/io/manip/flush
@Crthomas

I just noticed one pedantic thing: Name your header and implementation files exactly the same as the class name (DoubleStack.hpp and DoubleStack.cpp), abbreviating these might not be your friend in the future. Hopefully your IDE will do this for you by default.

I prefer to use .hpp for header files: it means the header file contains cpp code, as opposed to .h which could be C code. It's just a convention, but to me it makes sense :+)

Good Luck !!
Topic archived. No new replies allowed.