problem with const int inside class (c2258)

This is a discussion on problem with const int inside class (c2258) within the C++ Programming forums, part of the General Programming Boards category; having problems using the given .h and .template files for my lab... here is what errors i get: Code: Compiling... ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    problem with const int inside class (c2258)

    having problems using the given .h and .template files for my lab... here is what errors i get:

    Code:
    Compiling...
    lab_5.cpp
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(78) : error C2258: illegal pure syntax, must be '= 0'
            c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(95) : see reference to class template instantiation 'main_savitch_15::graph<Item>' being compiled
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(78) : error C2252: 'MAXIMUM' : pure specifier can only be specified for functions
            c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(95) : see reference to class template instantiation 'main_savitch_15::graph<Item>' being compiled
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(92) : error C2065: 'MAXIMUM' : undeclared identifier
            c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.h(95) : see reference to class template instantiation 'main_savitch_15::graph<Item>' being compiled
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.template(18) : error C2039: 'MAXIMUM' : is not a member of 'graph<Item>'
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.template(18) : error C2734: 'MAXIMUM' : const object must be initialized if not extern
    c:\documents and settings\all users\documents\school\nonlinear data structures\lab_5\graph.template(18) : error C2734: 'MAXIMUM' : const object must be initialized if not extern
    Error executing cl.exe.
    
    lab_5.exe - 6 error(s), 0 warning(s)
    seems it doesn't like the definition of MAXIMUM for some reason. here is the graph.h file where MAXIMUM is declared:

    Code:
    #ifndef MAIN_SAVITCH_GRAPH_H
    #define MAIN_SAVITCH_GRAPH_H
    #include <cstdlib>  // Provides size_t
    #include <set>      // Provides set
    
    namespace main_savitch_15
    {
        template <class Item>
        class graph
        {
        public:
            // MEMBER CONSTANTS
            const int MAXIMUM = 50;
            // CONSTRUCTOR
            graph( ) { many_vertices = 0; }
            // MODIFICATION MEMBER FUNCTIONS
            void add_vertex(const Item& label);
            void add_edge(int source, int target, int weight);
            void remove_edge(int source, int target);
            Item& operator [ ] (int vertex);
            // CONSTANT MEMBER FUNCTIONS
            int size( ) const { return many_vertices; }
            int is_edge(int source, int target) const;
            std::set<int> neighbors(int vertex) const;
            Item operator[ ] (int vertex) const;
        private:
            int edges[MAXIMUM][MAXIMUM];
            Item labels[MAXIMUM];
            int many_vertices;
        };
    }
    
    #include "graph.template" // Include the implementation.
    #endif
    we changed it to const int from static const int because the teacher said that VC++ before .NET had trouble with that or something... i tried changing it to static const int but got the same errors.

    here is the graph.template file if you want to check that out too:

    Code:
    #include <cassert>    // Provides assert
    #include <set>        // Provides set
    
    namespace main_savitch_15
    {
        template <class Item>
        const int graph<Item>::MAXIMUM;
    
        template <class Item>
        void graph<Item>::add_edge(int source, int target, int weight)
        // Library facilities used: cassert, cstdlib
        {
            assert(source < size( ));
            assert(target < size( ));
            edges[source][target] = weight;
        }
    
        template <class Item>
        void graph<Item>::add_vertex(const Item& label)
        // Library facilities used: cassert, cstdlib
        {
            int new_vertex_number;
            int other_number;
    
            assert(size( ) < MAXIMUM);
            new_vertex_number = many_vertices;
            many_vertices++;
            for (other_number = 0; other_number < many_vertices; ++other_number)
            {
                edges[other_number][new_vertex_number] = 32768;
                edges[new_vertex_number][other_number] = 32768;
            }
            labels[new_vertex_number] = label;
        }
    
        template <class Item>
        int graph<Item>::is_edge(int source, int target) const
        // Library facilities used: cassert, cstdlib
        {
            assert(source < size( ));
            assert(target < size( ));
            return edges[source][target]; //return "weight"
        }
    
        template <class Item>
        Item& graph<Item>::operator[ ] (int vertex)
        // Library facilities used: cassert, cstdlib
        {
            assert(vertex < size( ));
            return labels[vertex];     // Returns a reference to the label
        }
    
        template <class Item>
        Item graph<Item>::operator[ ] (int vertex) const
        // Library facilities used: cassert, cstdlib
        {
            assert(vertex < size( ));
            return labels[vertex];     // Returns only a copy of the label
        }
    
        template <class Item>
        std::set<int> graph<Item>::neighbors(int vertex) const
        // Library facilities used: cassert, cstdlib, set
        {
            std::set<int> answer;
            int i;
    
            assert(vertex < size( ));
    
            for (i = 0; i < size( ); ++i)
            {
                if (edges[vertex][i])
                    answer.insert(i);
            }
            return answer;
        }
    
        template <class Item>
        void graph<Item>::remove_edge(int source, int target)
        // Library facilities used: cassert, cstdlib
        {
            assert(source < size( ));
            assert(target < size( ));
            edges[source][target] = 32768;
        }
    }


    any suggestions would be appreciated...


    [edit]sorry bout the long lines in the error code![/edit]
    Last edited by talz13; 11-23-2003 at 04:17 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    It would be nice if compilers let you declare constants that way, but as a class is a type, rather than a real phisical object, there is no there there to store the constant. The class definition is really just a discription of one of the ways you are planing on using memory. The constructor is where all the action happens. If you have a const member variable you must set it's value in the constructor via an initalizer list, and only via this list. This, of course, changes our compile-time const into a rather boring and wasteful ordinary const but there are some hacks to get around this.

    A static on the other hand is simply a global variable that the compiler will hide as if it were a member function or field, but it's still a global variable.

    The correct way to handle statics is to pick one place in all the files that will one day be linked into your program and declare the space for the static there. At that time, and only that time if it is const static, you can give it an inital value.

    Code:
    // this can appear in as many compilation 
    // units as you want.
    template<class Item>
    class graph {
        static const int MAXIMUM;
    ...
    };
    ...
    //These next lines can appear only ONCE 
    //amongst all the files that you link 
    //together.
    
        template <class Item>
        const int graph<Item>::MAXIMUM=50;
    Hack #1 is to use an enum.
    Code:
    class graph {
    pubic:
    enum { MAXIMUM=50 }
    };
    Hack #2 is to use an int template paramiter with a resonable default. This actually verges on good programming, though a graph<int,20> and a graph<int,50> will be two completely different types.
    Code:
    template<class Item, int MAXIMUM=50>
    class graph {...
    In both cases you can just use MAXIMUM as if it were a const int, except you cannot un_const it with const_cast<>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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