Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Magic Code that Executes In Between Lines  (Read 2697 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Magic Code that Executes In Between Lines
« on: January 15, 2011, 09:23:23 pm »
As you can tell by the title, this error is a nasty one. I have no idea could possibly be causing it. Here is where it is happening:

Problem Area:
Code: [Select]
#include <SFML/Graphics.hpp>
#include "MainMenu.h"
#include "Globals.h"

Player Player1;                          // In between here
TroopManager TrMan;                // And here
ResourceManager ResMan;
TheCorePassage TCP;

int main()


You probably want to see the constructors now:
Player Constructor:
Code: [Select]
Player::Player()
{
    food = 1000;
    foodMax = 2000;
    wood = 500;
    woodMax = 1000;
    stone = 0;
    stoneMax = 0;
    gold = 0;
    goldMax = 0;
    population = 0;
    populationMax = 10;
    populationMaxFinal = 100;
    fame = 0;
    std::cout << "player" <<std::endl;
}


TroopManager Constructor:
Code: [Select]
TroopManager::TroopManager()
{
    std::cout << "troopmanager" << std::endl;
    SlaveID = 0;
    SwordID = 0;
    PikeID = 0;
    ArcherID = 0;
    SpearID = 0;
    CavalryID = 0;
    MastID = 0;
    SetUpgrades();
}


Notice how I put a cout statement at the end of Player and the beginning of TroopManager. I have a member inside of TroopManager that spawns units when activated. Well, that member isn't called until way later in the code. BUT! This program think I'm calling the member in between the object delcarations for Player and TroopManager.

Here is how I know. I put a cout statement inside of the member and it outputs Unit Spawned.

When I ran the program, the console window showed me this:
Quote
player
Unit Spawned
troopmanager


I put the player cout at the end of the constuctor and the troopmanager cout at the beginning of the second constructor. It's calling the uncalled member in between the two lines. D:

Please Help :)

If you need more information just say the word.

EDIT:
Here is what is inside of Globals.h
Globals.h
Code: [Select]
#ifndef GLOBALS_H_INCLUDED
#define GLOBALS_H_INCLUDED

#include "TheCorePassage.h"
#include "ResourceManager.h"
#include "Player.h"
#include "TroopManager.h"

extern Player Player1;
extern TroopManager TrMan;
extern ResourceManager ResMan;
extern TheCorePassage TCP;

#endif // GLOBALS_H_INCLUDED


EDIT:
Quote
It's calling the uncalled member in between the two lines. D:

It's actually not even calling the member... It is just creating a unit out of the blue. I tested it with some more cout statements.
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #1 on: January 15, 2011, 10:54:54 pm »
Pretty easy, The constructor in reality have every member in the initiation list which is run before actually calling the constructor resulting in that specific members constructor being called before the last output..

It's simply basic C++ and OOP.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #2 on: January 15, 2011, 11:35:12 pm »
I understand that, but I don't understand what is causing my error. :/
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #3 on: January 16, 2011, 12:01:18 am »
Like I said, it's not an error. This is how C++ works. You didn't specify any real error...

Let's explain it like this:

Code: [Select]

class A
{
public:
        A() { std::cout << "A constructed" << std::endl; }

};

class B
{
public:
        B() { std::cout << "B constructed" << std::endl; }

        A myA;
};


This will printout:
Code: [Select]
A constructed
B constructed


Even if you never referenced myA it will still be constructed and it's construction is inserted like this by the compiler:

Code: [Select]
B::B() : myA() { std::cout << "B constructed" << std::endl; }

So it will always run before B() is actually called.

Even without seeing your code this is the behavior I see from it. Correct me if I'm wrong, but what ever prints "Unit Spawned" is either a member of TroopManager or TroopManager inherits from that.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #4 on: January 16, 2011, 12:14:14 am »
OH! So it's an inheritance issue?
-Wander

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #5 on: January 16, 2011, 12:23:45 am »
If TroopManager inherits from whatever that prints unit spawned... then yes. The constructor of anything you inherits is inserted into the initialization list even if you don't do it yourself.  Just like with members.

But if you inherit from it, then why did you call it a member?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Magic Code that Executes In Between Lines
« Reply #6 on: January 16, 2011, 12:28:50 am »
In my edit at the very bottom of the post, I corrected myself a while ago.
-Wander