0

I'm trying to make a mini game in c++ and i have encountered a problem. I am currently trying to make a board for my game and i've made a function to draw the borders but i don't want to call it, i want it to be called by the constructor.

Board.h :

class Board
{
public:
    Board(Graphics& out_gfx);

private:
    void DrawBoardBorder();
    Graphics& in_gfx;
    Color borderColor = Colors::MakeRGB(94,35,113);

};

Board.cpp :

Board::Board(Graphics & out_gfx)
     :
     in_gfx(out_gfx)
{
     DrawBoardBorder();
}

The borders are drawn correctly if the function is moved to public and called with the help of the object, but the borders are not drawn if i let the constructor do the job. Why?

  • Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Feb 23 '18 at 13:55
  • 2
    Please **[edit]** your question with an [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Feb 23 '18 at 13:55
  • 1
    Is your application structured in such a way that you can call `DrawBoardBorder` once, or do you need to call it in a loop? (Presumably your constructor is only being called once.) – 0x5453 Feb 23 '18 at 13:59
  • I only need to call it once. –  Feb 23 '18 at 14:01
  • what compiler/version is being used ? – lostbard Feb 23 '18 at 14:03
  • 1
    @newbProgrammer *The borders are drawn correctly if the function is moved to public and called with the help of the object,* -- Why didn't you post this scenario you claim works? A description of what you're doing is not the same as seeing the actual code. – PaulMcKenzie Feb 23 '18 at 14:04
  • My crystal ball is telling me that you are not creating a `Board` object in your program, but you believe you are. Are you a victim of the [most vexing parse](https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work)? – PaulMcKenzie Feb 23 '18 at 14:11
  • *I didn't want to post the code because it's large* -- Meaning there is a logic flow issue far beyond the simple scenario you initially posted. If it were as simple as you had stated, then something like [this](https://www.ideone.com/r3cNss), would work, and it does. In the case of the code at the link, the code **is** called. So is your scenario that the function isn't called at all, or the function is called, but the border isn't showing up (which means maybe a color issue)? You need to do a little more debugging or at least more clarification as to the issue. – PaulMcKenzie Feb 23 '18 at 14:32
  • You're not really supposed to show large code on stackoverflow (although I guess that's better than nothing at all? not sure on that one). You should provide us with a MVCE (c.f. Nathan Olivier comment). That is, you should try to isolate your problem in as small a program as possible because a small program is easier to understand than a long program (either by removing/commenting out code incrementally until the error disappears, or by starting from scratch until the error appears). You may even solve the problem yourself that way, but if you don't, stackoverflow is here. – Caninonos Feb 23 '18 at 14:36
  • I'll try to simplify the code, keep you up to date (will take a while) –  Feb 23 '18 at 14:44
  • Are you passing the `Board` by reference or by copy? Without passing by reference, your constuctors will get called to make copies. – Thomas Matthews Feb 23 '18 at 17:45
  • No, it was passed by reference. Thanks for your attention everyone, turns out it was a framework logic mistake. I had a function called gfx.BeginFrame(). It's job is to clear the back buffer to get it ready to draw the next frame of the game. The constructor of Board is called before the gfx.BeginFrame() and so is erased ever before it is drawn. –  Feb 24 '18 at 11:19

0 Answers0