-4

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.

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
  • 3
    What's in `"relevantHeaders.h"` actually? Provide a [MCVE] instead of bogus code please! –  Jan 26 '18 at 17:24
  • 1
    @TheDude Come on now, it will make no difference - the code in question is very long and I don't think that crafting an example header file will assist anyone in answering this question. There is an answer posted below that is totally reasonable. All the header file does is make this `cpp` aware of `MyObject`. Would you rather I didn't include the `#include` line? I thought it more complete. I see in your post history that you typically have no issue with answering questions that do not give minmial, complete, and verifiable code ("bogus"). – pretzlstyle Jan 26 '18 at 17:31
  • 2
    _"the code in question is very long"_ That's why i asked for a ***minimal*** example that reproduces your problem. Do you want answers based on guesswork? –  Jan 26 '18 at 17:33
  • @TheDude And I said that I didn't see much value in crafting a full working (however minimal) example for the purposes of my question. I would then also have to give you the constructor of `MyObject`, the body of `doOtherStuff`, etc.. You're clearly knowledgeable, so you could very well offer me some advice here, but you'd rather play StackOverflow police for some kind of satisfaction. I'm asking a general question about a general concept - I'm not asking anyone to debug my code and tell me what characters to insert or delete. – pretzlstyle Jan 26 '18 at 17:37
  • 2
    @Anonymous: *"And I said that I didn't see much value in crafting a full working (however minimal) example for the purposes of my question."* - Then the question is off-topic on Stack Overflow. We need to be able to copy and paste your code into an editor and run the compiler, without editing a single line. – Christian Hackl Jan 26 '18 at 17:48
  • 2
    Re: "What does this mean?" -- it means that there is something wrong in `"relevantHeaders.h"`. If you refuse to talk about the contents of that header, nobody can tell you what the actual problem is. – Pete Becker Jan 26 '18 at 17:49
  • @ChristianHackl No, that is absolutely absurd. We all do paper and pencil problems, and visually look and and discuss code all the time in our classes. Do you not? You had no issue answering this question which contains code that is not runnable: https://stackoverflow.com/questions/35960074/string-allocation-in-c-why-does-this-work/35960510#35960510 – pretzlstyle Jan 26 '18 at 17:51
  • 1
    Did not compile on my machine, the relevantHeaders.h may be relevant. _Thoughts?_ I would not do it that way, but I'm not sure why you'd try to do it that way. And you may need `doOtherStuff(std::move(obj));` depending on doOtherStuff's signature. _What does this mean?_ Means that your example code does not compile, probably missing a header file. – Eljay Jan 26 '18 at 17:51
  • 1
    @Anonymous I am knowledgeable and professional enough not giving answers based on guesswork. These turn out to be mostly useless or plain wrong, that's what I've learned the last 30 years. I would give you the same response if you were a colleague attending me to ask that. Nothing to do with SO p'lice! –  Jan 26 '18 at 17:55
  • @Eljay Thanks for the comment. I'm trying to do it this way because it seemed to be a consensus as a good answer here: https://stackoverflow.com/questions/7557153/defining-an-object-without-calling-its-constructor-in-c – pretzlstyle Jan 26 '18 at 17:58
  • 1
    the error you show is definitely not coming from the code you show, so there is no way we can help you. Insisting that you dont need to show a mcve wont help anybody – 463035818_is_not_an_ai Jan 26 '18 at 17:58
  • 2
    @Anonymous: *"No, that is absolutely absurd."* - Stack Overflow disagrees with you. Actually, not only Stack Overflow. Back in the days of Usenet, the answer to such a question used to be "I don't have a crystal ball". Close-voting a question on SO is just the modern equivalent of posting a reply like that. *"You had no issue answering this question which contains code that is not runnable:"* - If only an `#include ` or `#include ` line is missing, then I'm sometimes (too?) generous. – Christian Hackl Jan 26 '18 at 17:59
  • 1
    in otherwords: if you show pseudo code and a pseudo error all you can expect is a pseudo answer ;) And thats not because we are trying to be nasty but because... well yes, because we dont have crystal balls to see what you are really running – 463035818_is_not_an_ai Jan 26 '18 at 18:01
  • 1
    @Anonymous _"The following (pseudo)code works perfectly fine:"_ [Sure?](http://coliru.stacked-crooked.com/a/9140bb421cd8e94d) –  Jan 26 '18 at 18:01
  • @TheDude I said pseudocode. I doubt your stated credentials if you are trying to compile pseudocode. Are you going to assert that you've never discussed some pseudocode on the board with a classmate or colleague? Did you tell them to get out of your sight until they could bring you a compilable example? – pretzlstyle Jan 26 '18 at 18:03
  • @tobi303 I will gladly accept pseudo answers that will give me some direction - that's what I'm after – pretzlstyle Jan 26 '18 at 18:04
  • 2
    @Anonymous: If all the effort you put into convincing us that this site's rules don't apply to *your* question had instead gone into producing an MCVE, then you'd probably have an answer to your question already. – Christian Hackl Jan 26 '18 at 18:04
  • 1
    @Anonymous _"Did you tell them to get out of your sight until they could bring you a compilable example? "_ Sure, I'm doing that all day long. I expect them to be at least as professional. Otherwise they'll probably not survive very long at our company. –  Jan 26 '18 at 18:07
  • @ChristianHackl If you put the effort you've spent into trying to convince me otherwise, then we could've had a fine discussion by now. There is one happening right now in the answer below in which I'm learning things, but you're too stubborn. This question is extremely similar to mine and has great discussions and upvotes, which goes to show that general questions offering only pseudo code can be totally appropriate and worthwhile. To deny that would be a delusion. – pretzlstyle Jan 26 '18 at 18:07
  • @ChristianHackl Forgot link in my previous comment: https://stackoverflow.com/questions/7557153/defining-an-object-without-calling-its-constructor-in-c – pretzlstyle Jan 26 '18 at 18:14
  • 2
    @Anonymous I'm saying that a last time: If you're asking for particular compiler errors (as in your second question's) part. Provide code that exactly reproduces that error. Crafting a minimal example that does so isn't rocket science. Regarding that _no constuctor call_ restriction you introduce, there's not enough information why you think that would be necessary at all, sounds rather a XY-problem for me. BTW, not that these questions you're linking to are quite old (2011), rules at this site may have changed since then. –  Jan 26 '18 at 18:16

1 Answers1

1

If creating a shallow copy is not enough for your objects, declare your own assignment operator. If shallow copies are fine, do nothing and an implicit shallow copy assignment operator will be made for you.

Then you can do:

MyObject obj;
if(things) {
    obj = MyObject(stuff);
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • I don't see why a shallow copy would ever not be enough in this case. I wouldn't have two different objects sharing the same shallow-copied values, it's just `obj`. In what cases would this be a problem? – pretzlstyle Jan 26 '18 at 17:44
  • _In what cases would this be a problem?_ Only in the case that a shallow copy is not enough for your MyObject's member variables. If MyObject does not have any objects or memory management (e.g., only contains `int` member variables), then it should be okay. – Eljay Jan 26 '18 at 17:56
  • 1
    @Anonymous read more on [Shallow Copy vs Deep Copy](https://stackoverflow.com/questions/2657810/deep-copy-vs-shallow-copy). A simple rule is that if you have any dynamic memory allocated by the class, you'll likely need a deep copy. See also [the Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) for when you should implement your own assignment operator. – scohe001 Jan 26 '18 at 18:04
  • @scohe001 Thanks. In my case, `MyObject` is very large, and includes stuff from dozens of headers, and not written by me. Which is why I'm trying to be careful. I'll read through the links you've posted and carefully inspect what's going on on my end. – pretzlstyle Jan 26 '18 at 18:09
  • @Anonymous perhaps I should reword the Rule of Three in a better way for this situation. Have you implemented a destructor for your `MyObject` class? If no and you don't have memory leaks, then the implicit shallow copy assignment operator is fine for you. Else, you'll need to declare your own. – scohe001 Jan 26 '18 at 18:22
  • @Anonymous Regardless though, the snippet I have in my answer should just **work** for any class you make. Making it work from the class side is your job as a class architect. Is there any specific reason you *need* a constructor call instead of just something like this? – scohe001 Jan 26 '18 at 18:23
  • @scohe001 The difficulty that I have to navigate is that the `MyObject` class is not mine, and I shouldn't really do anything to it. There is a very brief destructor there. It seems that my solution in the original question is the easiest, if it compiled :P – pretzlstyle Jan 26 '18 at 19:12
  • @Anonymous _" if it compiled :P"_ Well, here is my guesswork as you're insisting on that: You most probably missed to have a `#include ` statement in your mysterious `"relevantHeaders.h"` you're denying to show us. That's the most sophisticated guess I can do with the given information. –  Jan 26 '18 at 19:18
  • @TheDude Why can't I write the `include` right above `main`? Why must it be in the header? – pretzlstyle Jan 26 '18 at 19:37
  • @Anonymous _"Why must it be in the header? "_ When did I say so?? There's no need to be it in that header. How I'm supposed to know what you have in `"relevantHeaders.h"` actually? –  Jan 26 '18 at 19:39
  • @TheDude "Have you included the memory package" would have been sufficient, rather than everything else you said above. Regardless, adding the `include` did not change the compile error. – pretzlstyle Jan 26 '18 at 20:18
  • @Anonymous _"Regardless, adding the include did not change the compile error."_ Well, then we're back to the point why you ***must(!!!!)*** provide a [MCVE] that reproduces the problem, otherwise your question is considered unanswerable and should be closed! In case you were working aside me, I'd have that escalated to my boss, and complained about you as absolutely incompetent. –  Jan 26 '18 at 20:21
  • @TheDude if I was sitting right next to you I could just show you the _real_ code and headers, and we could sit there and troubleshoot and compile together – pretzlstyle Jan 26 '18 at 20:40
  • @Anonymous Sure, but you didn't ... You even said _that's not relevant ..._, _I don't see the value ..._ from the beginning. Return back to my very first comment. If you're resistant for advise, how should we help you, or getting you productive? –  Jan 26 '18 at 20:48