New to C++, Header File Error

This is a discussion on New to C++, Header File Error within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to C++ and I'm having trouble getting my header file to compile. Here's the code: Code: --------------------------------------------------------------------------------------------------------------------- ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    New to C++, Header File Error

    Hi, I'm new to C++ and I'm having trouble getting my header file to compile.
    Here's the code:
    Code:
    ---------------------------------------------------------------------------------------------------------------------
    
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class widget {
            private:
            char *label = NULL;
            int serNo;
    
            static int nExist, nCreated;
    
            public:
            widget(void);
            widget(const char *);
            widget(const widget&);
            ~widget();
    
            const widget & operator = (const widget&);
    
            void print(void) const;
    };
    
    #endif
    ---------------------------------------------------------------------------------------------------------------------\
    And heres the error:

    widget.h:11: error: ISO C++ forbids initialization of member `label'
    widget.h:11: error: making `label' static
    widget.h:11: error: invalid in-class initialization of static data member of non-integral type `char*'
    ----------------------------------------------------------------------------------------------------------------------

    Any help would be greatly appreciated
    Thanks

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by kspill2 View Post
    widget.h:11: error: ISO C++ forbids initialization of member `label'
    So, the compiler tells you it's line 11, and that it's forbidden to initialise member 'label'...
    I might be stating the obvious here, but if you remove the part that says: '= NULL' you'll be fine.
    Initialisation of member variables should be done in the constructor, only statics are initialised like that, but you can't initialise statics in a header, they need to be initialised in your c file.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Thanks

    Thanks for the help. Works fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

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