473,498 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL: assistance with error - class undefine type

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
May 19 '06 #1
7 1864
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).
May 19 '06 #2
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
May 19 '06 #3
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
May 19 '06 #4
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
May 19 '06 #5
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
May 19 '06 #6
In article <Jb******************************@garden.net>,
de********@hotmail.com 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.
May 19 '06 #7
Jerry Coffin wrote:
In article <Jb******************************@garden.net>,
de********@hotmail.com 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

May 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2697
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
6
1572
by: mar00ned | last post by:
Hi, I have a written a custom allocator for STL, on the lines of default allocator as follows : template <class T> class pool_allocator { public: typedef size_t size_type;
4
1445
by: joetekubi | last post by:
hello all, while working with STL containers and various class composition, I can across a bug that I can't seem to find. When using a STL container in a derived class with composition,...
10
4629
by: SpOiLeR | last post by:
I have function bool IsGood (const std::string& sr); I want to use that function in std::not1 STL functor. I tried this: not1(IsGood) /* error : 'std::unary_negate<_Fn1> std::not1(const...
4
3491
by: david.dfx | last post by:
-------------------------------------------------------------------------------- I'm having a problem implementing the STL map container using a class object. I'd like to use map to store a pair...
7
1969
by: rodrigostrauss | last post by:
I'm using Visual Studio 2005 Professional, and I didn't find the STL.NET. This code: #include "stdafx.h" #include <vector> using namespace System; using namespace std; int...
2
2158
by: mailarchis | last post by:
I am trying to write a header file in C++.It has some classes which make use of C++ STL.Say for example the following code foo.h template <class key,class val> class d_array { public :...
1
1777
by: Axel Gallus | last post by:
Hello, i have a question concerning STL non-standard hash_maps under Visual Studio 2005: Microsoft STL requires a "hash_compare" object for hash_maps: template <class Key, class Type, class...
7
3105
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.