There are a few questions I've seen on SO that deal with this issue, but I seem to be having a unique issue.
I am trying to create an object , then do some initializing elsewhere in my code, and call the constructor after having done that. To do this, I'm creating a smart pointer to an instance of the object, and then resetting it:
#include "relevantHeaders.h"
using namespace std;
int main(int argc, char *argv[])
{
std::unique_ptr<MyObject> obj;
if(someCondition){
// Do stuff
obj.reset(new MyObject(stuff));
}
doOtherStuff(obj);
}
The reason that I need to do this is because obj
continues to be used after the conditional under which I call the constructor- if I were to instead create the object, in a normal call, under the conditional, then it would be out of scope later.
First, this seemed to me to be the best way to do this, after some research. Thoughts?
Second, I am getting this error upon compiling:
The text ">" is unexpected. It may be that this token was intended as
a template argument list terminator but the name is not known to be a
template
What does this mean?
Edit: Since everyone is highly concerned with relevantHeaders.h
, let me say this... The following (pseudo)code works perfectly fine:
#include "relevantHeaders.h"
using namespace std;
int main(int argc, char *argv[])
{
MyObject obj;
if(someCondition){
// Do stuff
// obj.reset(new MyObject(stuff));
}
doOtherStuff(obj);
}
that is, I can use MyObject
just fine. It breaks when I add the std
utility unique_ptr
.