Constructor Initializer List Problem (New (to me) compiler warning)

This is a discussion on Constructor Initializer List Problem (New (to me) compiler warning) within the C++ Programming forums, part of the General Programming Boards category; I went to add one more initialization value to the poor overworked constructor of my bloated "shoulda planned a tad ...

  1. #1
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56

    Constructor Initializer List Problem (New (to me) compiler warning)

    I went to add one more initialization value to the poor overworked constructor of my bloated "shoulda planned a tad more on paper first" / "do everything" Actor class:

    Code:
    class Actor
    {
        public:
            Actor(double x = 0.0, double y = 0.0, double z = 0.0,
                  double xlb = -1000.0, double xub = 1000.0,
                  double ylb = -1000.0, double yub = 1000.0,
                  double zlb = -1000.0, double zub = 1000.0,
                  double sz = 1.0, int tf = 5) :
                xPos(x), yPos(y), zPos(z),
                xLowerBound(xlb), xUpperBound(xub),
                yLowerBound(ylb), yUpperBound(yub),
                zLowerBound(zlb), zUpperBound(zub),
                size(sz), ThoughtFrequency(tf) {}
            virtual ~Actor() {};
    ...
    The ThoughtFrequency(tf) at the end is what I added. Immediately I began to get this arcane (to me) compiler warning:

    Code:
    ||=== Fri Oct 21, Debug ===|
    /storage/Projects/Recent/Fri Oct 21/actor.h||In constructor ‘Actor::Actor(double, double, double, double, double, double, double, double, double, double, int)’:|
    /storage/Projects/Recent/Fri Oct 21/actor.h|99|warning: ‘Actor::size’ will be initialized after|
    /storage/Projects/Recent/Fri Oct 21/actor.h|78|warning:   ‘int Actor::ThoughtFrequency’|
    /storage/Projects/Recent/Fri Oct 21/actor.h|14|warning:   when initialized here|
    /storage/Projects/Recent/Fri Oct 21/actor.h||In constructor ‘Actor::Actor(double, double, double, double, double, double, double, double, double, double, int)’:|
    /storage/Projects/Recent/Fri Oct 21/actor.h|99|warning: ‘Actor::size’ will be initialized after|
    /storage/Projects/Recent/Fri Oct 21/actor.h|78|warning:   ‘int Actor::ThoughtFrequency’|
    /storage/Projects/Recent/Fri Oct 21/actor.h|14|warning:   when initialized here|
    ||=== Build finished: 0 errors, 6 warnings ===|
    And in a maybe unrelated coincidence - my program began to seg-fault intermittently on execution. I don't know why it'd matter what order these private variables are initialized in, so I must be misunderstanding something.
    (Additionally, in an unrelated commentary, I do plan to re-factor this all out from Class Actor into something like Entity->Camera , Entity->Actor, Actor->Player, Actor->Enemy eventually. Inheritance is newer to me. This won't always be a bloated mess.)
    Please, I'm afraid and alone - the walls of my computer room are turning black and rising up around me, shutting out the sun. As always I appreciate any incite from the community here.

    - Matt

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Your member variables will be initialised in the order of their declaration, regardless of the order you write in the initialiser list. Your compiler is just warning you of this fact, because you presumably declared size after ThoughtFrequency.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    Of course lazerlight, you were right - I moved ThoughtFrequency below size in my list of private variables and the error cleared right up. I still get random seg-faults, but at least now it's clear to me it's unrelated to this error.

    Thank you! And, happy Diwali day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warning: excess elements in scalar initializer
    By TeRMaL in forum C Programming
    Replies: 9
    Last Post: 10-08-2010, 03:54 AM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. Replies: 10
    Last Post: 08-27-2009, 09:58 AM
  4. Replies: 12
    Last Post: 01-17-2009, 03:35 AM
  5. constructor's initializer list: order of evaluation
    By rpet in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 06:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21