I am trying to use the stdlib stack in a class that I have created, but I am having issues creating it dynamically.
Here is the relevant code from my header file “matcher.h”:
private:
stack<char> opens;
and here is the constructor that I am creating that only allocates the stack:
#include "matcher.h"
using namespace std;
//Creates a matcher object with the default values.
matcher::matcher()
{
opens = new stack<char>;
}
The error I am getting is below:
matcher.cpp:19:17: error: no match for ‘operator=’ in ‘((matcher*)this)->matcher::opens = ((*(const std::deque<char, std::allocator<char> >*)(& std::deque<char, std::allocator<char> >())), (operator new(40u), (<statement>, ((std::stack<char>*)<anonymous>))))’
This says to me that the std::stack does not contain an assignment operator, which leads me to my question:
What method should I use in order to get a stack that will persist within my matcher object if it does not contain an assignment operator?
Thank you for your time.
opensis an object within the class, and so does not need to be allocated withnew.If you want it to be default-constructed, then that will happen automatically – you don’t need to write any code to do that. If all the class members can be default constructed, then you don’t need to write a default-constructor for the class at all.
If you have a member that can’t (or shouldn’t) be default constructed, then you do that in the constructor’s initialiser list, for example:
Only use
newwhen you really need a dynamically allocated object, and make sure it gets deleted once you’ve finished with it – preferably using RAII, since it’s often hard to get it right any other way. If you want it to be tied to the lifetime of another object, then just place it inside that class as a member and don’t bother with dynamic allocation.