STL: assistance with error - class undefine type

Discussion in 'C++' started by Alden Pierre, May 19, 2006.

  1. Alden Pierre

    Alden Pierre Guest

    Hello,

    I'm trying to create my own user define container, but I'm having a
    little hard time figuring out why is my class considered undefined by my
    compiler. Here is the following code.

    // file pos_neg_array.h

    #ifndef FILE_POS_NEG_ARRAY
    #define FILE_POS_NEG_ARRAY
    #include <iterator>
    #include <algorithm>
    #include <iostream>

    using namespace std;

    template< class T, class Allocator = allocator <T> >
    class Pos_Neg_Array {
    private:

    T * ptr;
    unsigned zero;
    unsigned pos_extent;
    unsigned neg_extent;
    Allocator a;

    public:

    typedef T valuetype;
    typedef T allocator_type;
    typedef T & reference;
    typedef const T & const_reference;
    typedef int size_type;
    typedef int difference_type;
    typedef T * pointer;
    typedef const T * const_pointer;

    // constructor
    Pos_Neg_Array()
    };
    #endif

    // implementation file pos_neg_array.cpp

    #include "pos_neg_array.h"

    template <class T, class Allocator = allocator <T> >
    Pos_Neg_Array<T>::pos_Neg_Array()
    {

    }


    Regards,
    Alden
     
    Alden Pierre, May 19, 2006
    #1
    1. Advertising

  2. Alden Pierre

    Mark P Guest

    Alden Pierre wrote:
    > Hello,
    >
    > I'm trying to create my own user define container, but I'm having a
    > little hard time figuring out why is my class considered undefined by my
    > compiler. Here is the following code.


    It would be a lot more helpful if you showed exactly what the compiler said.

    >
    > // file pos_neg_array.h
    >
    > #ifndef FILE_POS_NEG_ARRAY
    > #define FILE_POS_NEG_ARRAY
    > #include <iterator>
    > #include <algorithm>
    > #include <iostream>
    >
    > using namespace std;
    >
    > template< class T, class Allocator = allocator <T> >
    > class Pos_Neg_Array {
    > private:
    >
    > T * ptr;
    > unsigned zero;
    > unsigned pos_extent;
    > unsigned neg_extent;
    > Allocator a;
    >
    > public:
    >
    > typedef T valuetype;
    > typedef T allocator_type;


    Are you sure about this?

    > typedef T & reference;
    > typedef const T & const_reference;
    > typedef int size_type;
    > typedef int difference_type;
    > typedef T * pointer;
    > typedef const T * const_pointer;
    >
    > // constructor
    > Pos_Neg_Array()


    Error, missing semicolon

    > };
    > #endif
    >
    > // implementation file pos_neg_array.cpp
    >
    > #include "pos_neg_array.h"
    >
    > template <class T, class Allocator = allocator <T> >
    > Pos_Neg_Array<T>::pos_Neg_Array()
    > {
    >
    > }
    >


    I'm still not clear what your problem is. As a possibly helpful piece
    of advice, you will generally need to include the implementation of a
    template class in the header file (there are exceptions and
    workarounds-- see the FAQ for more).
     
    Mark P, May 19, 2006
    #2
    1. Advertising

  3. Re: assistance with error - class undefine type

    Alden Pierre wrote:
    > I'm trying to create my own user define container, but I'm having a
    > little hard time figuring out why is my class considered undefined by
    > my compiler. Here is the following code.


    This is covered in the FAQ 5.8.

    >
    > // file pos_neg_array.h
    >
    > #ifndef FILE_POS_NEG_ARRAY
    > #define FILE_POS_NEG_ARRAY
    > #include <iterator>
    > #include <algorithm>
    > #include <iostream>
    >
    > using namespace std;


    This is very dangerous. Considrer rethinking adding 'using' directives
    to header files.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 19, 2006
    #3
  4. Alden Pierre

    Alden Pierre Guest

    Mark P wrote:
    > Alden Pierre wrote:
    >> Hello,
    >>
    >> I'm trying to create my own user define container, but I'm having
    >> a little hard time figuring out why is my class considered undefined
    >> by my compiler. Here is the following code.

    >
    > It would be a lot more helpful if you showed exactly what the compiler
    > said.
    >
    >>
    >> // file pos_neg_array.h
    >>
    >> #ifndef FILE_POS_NEG_ARRAY
    >> #define FILE_POS_NEG_ARRAY
    >> #include <iterator>
    >> #include <algorithm>
    >> #include <iostream>
    >>
    >> using namespace std;
    >>
    >> template< class T, class Allocator = allocator <T> >
    >> class Pos_Neg_Array {
    >> private:
    >>
    >> T * ptr;
    >> unsigned zero;
    >> unsigned pos_extent;
    >> unsigned neg_extent;
    >> Allocator a;
    >>
    >> public:
    >>
    >> typedef T valuetype;
    >> typedef T allocator_type;

    >
    > Are you sure about this?
    >
    >> typedef T & reference;
    >> typedef const T & const_reference;
    >> typedef int size_type;
    >> typedef int difference_type;
    >> typedef T * pointer;
    >> typedef const T * const_pointer;
    >>
    >> // constructor
    >> Pos_Neg_Array()

    >
    > Error, missing semicolon
    >
    >> };
    >> #endif
    >>
    >> // implementation file pos_neg_array.cpp
    >>
    >> #include "pos_neg_array.h"
    >>
    >> template <class T, class Allocator = allocator <T> >
    >> Pos_Neg_Array<T>::pos_Neg_Array()
    >> {
    >>
    >> }
    >>

    >
    > I'm still not clear what your problem is. As a possibly helpful piece
    > of advice, you will generally need to include the implementation of a
    > template class in the header file (there are exceptions and
    > workarounds-- see the FAQ for more).


    I apologize for not including the error.

    // error report

    pos_neg_array.cpp:4: error: invalid use of undefined type `class
    Pos_Neg_Array<T, std::allocator<_CharT> >'
    pos_neg_array.h:10: error: declaration of `class Pos_Neg_Array<T,
    std::allocator<_CharT> >'
    pos_neg_array.cpp:4: error: default template arguments may not be used
    in function templates
    pos_neg_array.cpp:4: error: template definition of non-template
    `Pos_Neg_Array<T, std::allocator<_CharT> >::pos_Neg_Array()'
    *** Error code 1

    // --------------------------------------------------------

    Regards,
    Alden
     
    Alden Pierre, May 19, 2006
    #4
  5. Alden Pierre

    Alden Pierre Guest

    Mark P wrote:
    > Alden Pierre wrote:
    >> Hello,
    >>
    >> I'm trying to create my own user define container, but I'm having
    >> a little hard time figuring out why is my class considered undefined
    >> by my compiler. Here is the following code.

    >
    > It would be a lot more helpful if you showed exactly what the compiler
    > said.
    >
    >>
    >> // file pos_neg_array.h
    >>
    >> #ifndef FILE_POS_NEG_ARRAY
    >> #define FILE_POS_NEG_ARRAY
    >> #include <iterator>
    >> #include <algorithm>
    >> #include <iostream>
    >>
    >> using namespace std;
    >>
    >> template< class T, class Allocator = allocator <T> >
    >> class Pos_Neg_Array {
    >> private:
    >>
    >> T * ptr;
    >> unsigned zero;
    >> unsigned pos_extent;
    >> unsigned neg_extent;
    >> Allocator a;
    >>
    >> public:
    >>
    >> typedef T valuetype;
    >> typedef T allocator_type;

    >
    > Are you sure about this?
    >
    >> typedef T & reference;
    >> typedef const T & const_reference;
    >> typedef int size_type;
    >> typedef int difference_type;
    >> typedef T * pointer;
    >> typedef const T * const_pointer;
    >>
    >> // constructor
    >> Pos_Neg_Array()

    >
    > Error, missing semicolon
    >
    >> };
    >> #endif
    >>
    >> // implementation file pos_neg_array.cpp
    >>
    >> #include "pos_neg_array.h"
    >>
    >> template <class T, class Allocator = allocator <T> >
    >> Pos_Neg_Array<T>::pos_Neg_Array()
    >> {
    >>
    >> }
    >>

    >
    > I'm still not clear what your problem is. As a possibly helpful piece
    > of advice, you will generally need to include the implementation of a
    > template class in the header file (there are exceptions and
    > workarounds-- see the FAQ for more).

    I apologize for not including the error.

    // error report

    pos_neg_array.cpp:4: error: invalid use of undefined type `class
    Pos_Neg_Array<T, std::allocator<_CharT> >'
    pos_neg_array.h:10: error: declaration of `class Pos_Neg_Array<T,
    std::allocator<_CharT> >'
    pos_neg_array.cpp:4: error: default template arguments may not be used
    in function templates
    pos_neg_array.cpp:4: error: template definition of non-template
    `Pos_Neg_Array<T, std::allocator<_CharT> >::pos_Neg_Array()'
    *** Error code 1

    // --------------------------------------------------------

    Regards,
    Alden
     
    Alden Pierre, May 19, 2006
    #5
  6. Alden Pierre

    Mark P Guest

    Alden Pierre wrote:
    > Mark P wrote:
    >> Alden Pierre wrote:
    >>> Hello,
    >>>
    >>> I'm trying to create my own user define container, but I'm having
    >>> a little hard time figuring out why is my class considered undefined
    >>> by my compiler. Here is the following code.

    >>
    >> It would be a lot more helpful if you showed exactly what the compiler
    >> said.
    >>
    >>>
    >>> // file pos_neg_array.h
    >>>
    >>> #ifndef FILE_POS_NEG_ARRAY
    >>> #define FILE_POS_NEG_ARRAY
    >>> #include <iterator>
    >>> #include <algorithm>
    >>> #include <iostream>
    >>>
    >>> using namespace std;
    >>>
    >>> template< class T, class Allocator = allocator <T> >
    >>> class Pos_Neg_Array {
    >>> private:
    >>>
    >>> T * ptr;
    >>> unsigned zero;
    >>> unsigned pos_extent;
    >>> unsigned neg_extent;
    >>> Allocator a;
    >>>
    >>> public:
    >>>
    >>> typedef T valuetype;
    >>> typedef T allocator_type;

    >>
    >> Are you sure about this?
    >>
    >>> typedef T & reference;
    >>> typedef const T & const_reference;
    >>> typedef int size_type;
    >>> typedef int difference_type;
    >>> typedef T * pointer;
    >>> typedef const T * const_pointer;
    >>>
    >>> // constructor
    >>> Pos_Neg_Array()

    >>
    >> Error, missing semicolon
    >>
    >>> };
    >>> #endif
    >>>
    >>> // implementation file pos_neg_array.cpp
    >>>
    >>> #include "pos_neg_array.h"
    >>>
    >>> template <class T, class Allocator = allocator <T> >
    >>> Pos_Neg_Array<T>::pos_Neg_Array()
    >>> {
    >>>
    >>> }
    >>>

    >>
    >> I'm still not clear what your problem is. As a possibly helpful piece
    >> of advice, you will generally need to include the implementation of a
    >> template class in the header file (there are exceptions and
    >> workarounds-- see the FAQ for more).

    > I apologize for not including the error.
    >
    > // error report
    >
    > pos_neg_array.cpp:4: error: invalid use of undefined type `class
    > Pos_Neg_Array<T, std::allocator<_CharT> >'
    > pos_neg_array.h:10: error: declaration of `class Pos_Neg_Array<T,
    > std::allocator<_CharT> >'
    > pos_neg_array.cpp:4: error: default template arguments may not be used
    > in function templates
    > pos_neg_array.cpp:4: error: template definition of non-template
    > `Pos_Neg_Array<T, std::allocator<_CharT> >::pos_Neg_Array()'
    > *** Error code 1
    >
    > // --------------------------------------------------------
    >


    The meaning is slightly obscure I think, but the problem is your
    function definition in the .cpp file.

    Try:

    template <class T, class Allocator>
    Pos_Neg_Array<T,Allocator>::pos_Neg_Array() {}

    That is, don't repeat the template defaults in the member function
    definition and do include all template parameters in the class name
    preceding the double colon.

    -Mark
     
    Mark P, May 19, 2006
    #6
  7. Alden Pierre

    Jerry Coffin Guest

    In article <>,
    says...
    > Hello,
    >
    > I'm trying to create my own user define container, but I'm having a
    > little hard time figuring out why is my class considered undefined by my
    > compiler. Here is the following code.


    [ ... ]

    > using namespace std;


    As already noted, this is bad idea.

    > template< class T, class Allocator = allocator <T> >
    > class Pos_Neg_Array {
    > private:


    This is redundant -- members of a class are private by
    default.

    [ ... ]

    > // constructor
    > Pos_Neg_Array()
    > };


    You have to do one of two things: either declare your
    ctor (which ends in a semicolon) or else define it (put a
    body on the end). For most C++ compilers, you'll want to
    make it a definition:

    Pos_Neg_Array() {}

    > // implementation file pos_neg_array.cpp
    >
    > #include "pos_neg_array.h"
    >
    > template <class T, class Allocator = allocator <T> >
    > Pos_Neg_Array<T>::pos_Neg_Array()
    > {
    >
    > }


    You want to put the default argument only on the class
    definition, not on the definition of the member function
    (s). If you want to put your ctor into a separate source
    file, you'll need to export it:

    // pos_neg_array.h
    #include <memory>

    export template<
    class T,
    class Allocator=std::allocator <T>
    >

    class Pos_Neg_Array {
    T * ptr;
    unsigned zero;
    unsigned pos_extent;
    unsigned neg_extent;
    Allocator a;

    public:

    typedef T valuetype;
    typedef T allocator_type;
    typedef T & reference;
    typedef const T & const_reference;
    typedef int size_type;
    typedef int difference_type;
    typedef T * pointer;
    typedef const T * const_pointer;

    Pos_Neg_Array();
    };
    #endif

    // pos_neg_array.cpp
    #include "pos_neg_array.h"

    template <class T, class Allocator>
    Pos_Neg_Array<T, Allocator>::pos_Neg_Array() {}

    Then the important point: you'll only be able to compile
    this code with a compiler that supports the export
    keyword. Right now, that mostly means Comeau's compiler.
    There are persistent rumors that the Intel compiler has
    undocumented support as well. This sounds halfway
    reasonable, since both of these compilers are built
    around the EDG C++ front-end. OTOH, Comeau also uses a
    pre-linker to help out by re-invoking the compiler as
    needed to instantiate the exported parts of the template
    over the correct types as needed. I'm not at all sure how
    (or even if) Intel handles that.

    In any case, I don't know of any other compiler that even
    attempts to implement export -- thus the advice above
    that for most compilers, you just want to make the ctor
    an inline function in the header and be done with it.
    Given that the body of the ctor is empty, you'd probably
    prefer that it be an inline function anyway. OTOH,
    presumably at some point you'd want to add a few things
    like allocating storage there, at which point exporting
    it might start to make sense (if you can afford to ignore
    compilers that don't support it).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, May 19, 2006
    #7
  8. Alden Pierre

    Greg Guest

    Jerry Coffin wrote:
    > In article <>,
    > says...
    > > Hello,
    > >
    > > I'm trying to create my own user define container, but I'm having a
    > > little hard time figuring out why is my class considered undefined by my
    > > compiler. Here is the following code.

    >
    > [ ... ]
    >
    > > using namespace std;

    >
    > As already noted, this is bad idea.
    >
    > > template< class T, class Allocator = allocator <T> >
    > > class Pos_Neg_Array {
    > > private:

    >
    > This is redundant -- members of a class are private by
    > default.


    The multiple, consecutive white space characters that not only surround
    "private:" but which appear throughout the entire class template
    definition are also "redundant" - but strangely not one of these
    unneeded spaces, tabs or carriage returns is subject to the same
    criticism leveled against the use of the label "private" - even though
    the total amount of unneeded white space far surpasses the eight
    redundant characters (including the colon) in the label "private:".

    But just as it is a good idea to use white space in a program (whether
    such white space is needed or not), it's also a good idea to label
    access levels of class members explicitly (whether such labels are
    needed or not). Explicit access labels provide evidence that the access
    level governing declarations within a class or struct is in fact by
    intention; whereas an access level assigned in the absence of a label
    could either be by intention or could be an oversight on the part of
    the programmer.

    Greg
     
    Greg, May 20, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmljaA==?=
    Replies:
    15
    Views:
    8,111
    aparnalr
    Sep 14, 2011
  2. Rafal 'Raf256' Maj

    undefine all macros from a file

    Rafal 'Raf256' Maj, Jan 19, 2005, in forum: C++
    Replies:
    1
    Views:
    3,020
    Jonathan Turkanis
    Jan 19, 2005
  3. Pietro Cerutti

    undefine for functions

    Pietro Cerutti, Aug 30, 2007, in forum: C Programming
    Replies:
    8
    Views:
    400
    Eric Sosman
    Aug 31, 2007
  4. T Ryi

    Undefine a function from header file

    T Ryi, Mar 24, 2010, in forum: C Programming
    Replies:
    6
    Views:
    742
    Kenny McCormack
    Mar 25, 2010
  5. Adriano Mitre
    Replies:
    7
    Views:
    167
    Adriano Mitre
    Nov 7, 2006
Loading...

Share This Page