Why does a const int member in class produce error?

This is a discussion on Why does a const int member in class produce error? within the C++ Programming forums, part of the General Programming Boards category; I have this const int private member in a class that produces an "uninitialized error" in the compiler for some ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Why does a const int member in class produce error?

    I have this const int private member in a class that produces an "uninitialized error" in the compiler for some reason, and I don't know why. At first I thought it was because I didn't assign it a value in the constructor, but then I added the code to do that, and it still gives the same error.

    Any ideas?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    A const member (or variable) cannot be assigned values.

    You have to initialize it inline (in which case it must be static)
    Code:
    private:
         static const int A = 5;
    Or in an initialization list.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by cyberfish View Post
    A const member (or variable) cannot be assigned values.

    You have to initialize it inline (in which case it must be static)
    Code:
    private:
         static const int A = 5;
    Or in an initialization list.
    Thanks, that worked.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Code:
    class foo
    {
    public:
        foo(): m_foo(0) {}
    protected:
        const int m_foo;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Producer/Consumer - problems creating threads
    By hansel13 in forum C Programming
    Replies: 47
    Last Post: 08-20-2010, 03:02 PM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 04:01 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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