In-class constant

This is a discussion on In-class constant within the C++ Programming forums, part of the General Programming Boards category; I put the title into the search engine and it turned up very little. Ok so I only looked at ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    In-class constant

    I put the title into the search engine and it turned up very little. Ok so I only looked at the first page but I'm tired.

    Here's the class - don't ask what it does because I don't know, I'm just fiddling with some hashing (thanks to Prelude for the tut. on her site):

    Code:
    template <class T> class database {
    public:
            database (int max_index_data);
            ~database ();
    
    private:
            const int       MAX_INDEX_DATA;
            unsigned        index[MAX_INDEX_DATA];
            T               data[MAX_INDEX_DATA];
    };
    
    template <class T> database<T>::database (int max_index_data) 
            :MAX_INDEX_DATA(max_index_data)
    {
            
    }
    
    template <class T> database<T>::~database ()
    {
    
    }
    Here are the errors which disgraced my output window

    Code:
    ------ Build started: Project: database_bollocks, Configuration: Debug Win32 ------
    
    Compiling...
    database_bollocks.cpp
    c:\Documents and Settings\lee\Desktop\database_bollocks\database_bollocks.cpp(14) : error C2327: 'database<T>::MAX_INDEX_DATA' : is not a type name, static, or enumerator
            c:\Documents and Settings\lee\Desktop\database_bollocks\database_bollocks.cpp(16) : see reference to class template instantiation 'database<T>' being compiled
    c:\Documents and Settings\lee\Desktop\database_bollocks\database_bollocks.cpp(14) : error C2065: 'MAX_INDEX_DATA' : undeclared identifier
    c:\Documents and Settings\lee\Desktop\database_bollocks\database_bollocks.cpp(15) : error C2327: 'database<T>::MAX_INDEX_DATA' : is not a type name, static, or enumerator
    c:\Documents and Settings\lee\Desktop\database_bollocks\database_bollocks.cpp(15) : error C3861: 'MAX_INDEX_DATA': identifier not found, even with argument-dependent lookup
    
    Build Time 0:00
    Build log was saved at "file://c:\Documents and Settings\lee\Desktop\database_bollocks\Debug\BuildLog.htm"
    database_bollocks - 4 error(s), 0 warning(s)
    
    
    ---------------------- Done ----------------------
    
        Build: 0 succeeded, 1 failed, 0 skipped
    Sorry for the slightly profane project name.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think MAX_INDEX_DATA needs to be static or else you need to use dynamic allocation.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The size of the object must be known at compile time, and it must be constant across all objects. But by using a per-object constant for the size of the embedded array, you'd have objects of different size. That's obviously not possible. JaWiB is right: either make the constant static and thus the same across all instances, or allocate dynamically.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    OK cheers guys
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 02:52 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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