Thread: initializing structure in a class

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    initializing structure in a class

    I'm doing an practice exercise from a book and I am having problems with initializing an array structure in a class.
    I get multiple errors. one says a brace-enclosed initializer is not allowed here before '{' token.
    another says ISO c++ forbids initialization of member 'drink'
    third -making 'drink' satic
    fourth error -invalid in class initialization of static data member of non integral type drinkInfo[3]
    Can you help me find the error in the code?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    struct drinkInfo{
        string name;
        double price;
        int quantity;
    
        drinkInfo(string n, double p, int q){
            name = n;
            price = p;
            quantity = q;
        }
    
    };
    
    
    class machine {
        private:
            drinkInfo drink[3] = { drinkInfo("cola", 0.75, 20),
                                   drinkInfo("sprite", 0.75, 20),
                                   drinkInfo("root beer", 0.75, 20) };
    
    };

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    You can't initialize data in the class definition: that's what the constructor is for. In this case, you probably want to use a const object, which must also be initialized in construction.

    The only one that's different are static consts. As it certainly can't be initialized on construction because it's static, a static const has slightly different syntax:

    Code:
    class machine {
        private:
            static const drinkInfo drink[3];
    };
    
    const drinkInfo machine::drink[3] = { drinkInfo("cola", 0.75, 20),
                                            drinkInfo("sprite", 0.75, 20),
                                            drinkInfo("root beer", 0.75, 20) };
    Hope it helps.
    Last edited by Thinias; 05-19-2010 at 11:42 PM. Reason: Clarifying intent.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    You need to initialize it in constructor. Here you've to declare it only.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    8
    thanks Thinias that got it working. Is it possible to have a structure inside a class?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by diego View Post
    Is it possible to have a structure inside a class?
    Yes (in fact, classes are structures, and you can have one struct inside another).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure vs. class
    By $l4xklynx in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 03:00 AM
  2. Abstract class (IDispatch) in a C structure
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2008, 08:38 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM