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

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04: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