STL: assistance with error - class undefine type

A

Alden Pierre

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
 
M

Mark P

Alden said:
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).
 
V

Victor Bazarov

Alden said:
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
 
A

Alden Pierre

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


Are you sure about this?


Error, missing semicolon


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
 
A

Alden Pierre

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


Are you sure about this?


Error, missing semicolon


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
 
M

Mark P

Alden said:
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
 
J

Jerry Coffin

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 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).
 
G

Greg

Jerry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top