//my .h file:
#include <stdlib.h>
usingnamespace 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.
intoperator==(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]
You wrote, usingnamespace 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.
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?
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:
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 :+)