Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL: assistance with error - class undefine type

Reply
Thread Tools

STL: assistance with error - class undefine type

 
 
Alden Pierre
Guest
Posts: n/a
 
      05-19-2006
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>:os_Neg_Array()
{

}


Regards,
Alden
 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      05-19-2006
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>:os_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).
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-19-2006
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


 
Reply With Quote
 
Alden Pierre
Guest
Posts: n/a
 
      05-19-2006
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>:os_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> >:os_Neg_Array()'
*** Error code 1

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

Regards,
Alden
 
Reply With Quote
 
Alden Pierre
Guest
Posts: n/a
 
      05-19-2006
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>:os_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> >:os_Neg_Array()'
*** Error code 1

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

Regards,
Alden
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      05-19-2006
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>:os_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> >:os_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>:os_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
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      05-19-2006
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>:os_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>:os_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.
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      05-20-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2.0 runtime javascript error - WebForm_PostBackOptions is undefine =?Utf-8?B?UmljaA==?= ASP .Net 15 09-14-2011 09:04 AM
Undefine a function from header file T Ryi C Programming 6 03-25-2010 01:36 PM
undefine for functions Pietro Cerutti C Programming 8 08-31-2007 01:25 AM
how to reset and how to undefine a class Adriano Mitre Ruby 7 11-07-2006 10:06 AM
undefine all macros from a file Rafal 'Raf256' Maj C++ 1 01-19-2005 05:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57