Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size) : wxFrame(NULL, wxID_ANY, title, pos, size) this->panel = new wxPanel(this, wxID_ANY); controlChoice this->profileChoice; this->profileChoice->choice = new wxChoice(this->panel, wxID_ANY);

On the line "controlChoice this->profileChoice;" I receive "error: expected an identifier" and it's the "this" that's showing the error.

EDIT: removed the "does compile". Possibly it wasn't removing the previously successfully compiled executable and I just kept loading that.

It's unlikely this code compile, actually. controlChoice this->profileChoice is not valid C++. What are you trying to do? Etienne de Martel Oct 21, 2022 at 17:23 Explain in your own words what the line controlChoice this->profileChoice; should do. That's a typename followed by a this -quantified field, which is not valid C++ syntax. Silvio Mayolo Oct 21, 2022 at 17:23 Can you explain why you need the this-> syntax? I've written dozens of C++ programs and never used the this-> syntax. Thomas Matthews Oct 21, 2022 at 17:37 @ThomasMatthews The only place this is needed in the given program is when writing new wxPanel(this, wxID_ANY) , everywhere else(in the shown sample) it is optional. But this-> is not needed. Maybe OP wanted to be explicit. But then they ended up writing incorrect code like controlChoice this->profileChoice; . user12002570 Oct 21, 2022 at 17:50

The code does compile.

That is not valid C++ syntax and any conformant C++ compiler should reject it as controlChoice is a typename while this->profileChoice is a qualified name and C++ grammar rules doesn't allow such an usage.

You can instead use the member initializer list for initialization of those members:

class MyFrame : public wxFrame
public:
    MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
private:
    struct controlChoice
        wxChoice *choice;
        int prevChoice;
        //add a constructor
        controlChoice(wxChoice *pChoice, int pPrevChoice): choice(pChoice), 
                                                           prevChoice(pPrevChoice)
    wxPanel *panel;
    controlChoice *profileChoice;
MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
    : wxFrame(NULL, wxID_ANY, title, pos, size), 
      panel(new wxPanel(this, wxID_ANY)),
      profileChoice(new controlChoice(new wxChoice(this->panel, wxID_ANY), 5))
    //empty body as we did the initialization in the member initializer list

Note that initialization can be done in the member initializaer as opposed to assignment which is done in the constructor body. And since in the modified program we've used the member initializer list, we don't need to do anything else in the constructor body.

Thank you for the response. On the profileChoice example you provided, I'm getting "a value of type "wxChoice *" cannot be used to initialize an entity of type "MyFrame::controlChoice *" ... It needs to be profileChoice.choice(new...) right? But the proFileChoice would have to be initialized first? – Chemdream Oct 21, 2022 at 17:53 @Chemdream You're welcome. Actually you need to add a constructor to your class controlChoice so that you can initialize profileChoice. I will update the answer to show that in a minute. – user12002570 Oct 21, 2022 at 17:57 Ah. I'm coming from Golang. That might be confusing me. Because I controlChoice is a struct. That's why I thought I'd initialize it in the MyFrame::MyFrame. I didn't realize structs needed a constructor. – Chemdream Oct 21, 2022 at 18:00 @Chemdream I've updated the answer. Try the updated code. You can modify it according to your requirements. – user12002570 Oct 21, 2022 at 18:04 Thanks so much. Errors are already disappearing. I'll school myself in struct constructors and check this one as the answer. Sincerely, thank you again! – Chemdream Oct 21, 2022 at 18:12

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.