473,666 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template vector, incomplete type error

Hi,
I am migrating some std::vectors to use a template instead, but I get
an incomplete type error in a struct declaration.

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec():Vec<T>() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};
typedef Vec<doublevecdb l;
typedef Vec<vecdblmatdb l;
#include "matvec.h"
// that is it above

typedef vecdbl memvec;

struct felt
{
...
memvec m; // <- incomplete type error

Sep 30 '06 #1
13 2250
In article <11************ **********@c28g 2000cwb.googleg roups.com>,
<im*****@hotmai l.co.ukwrote:
>Hi,
I am migrating some std::vectors to use a template instead, but I get
an incomplete type error in a struct declaration.

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec():Vec<T>() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};
typedef Vec<doublevecdb l;
typedef Vec<vecdblmatdb l;
#include "matvec.h"
// that is it above

typedef vecdbl memvec;

struct felt
{
..
memvec m; // <- incomplete type error
I don't understand Vec():Vec<T>() and probably you're compiler
doesn't either eventually makeing Vec<Tincomplete classes
because of that problem.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 30 '06 #2
In article <ef**********@p anix1.panix.com >,
Greg Comeau <co****@comeauc omputing.comwro te:
>In article <11************ **********@c28g 2000cwb.googleg roups.com>,
<im*****@hotmai l.co.ukwrote:
>>Hi,
I am migrating some std::vectors to use a template instead, but I get
an incomplete type error in a struct declaration.

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec():Vec<T>() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};
typedef Vec<doublevecdb l;
typedef Vec<vecdblmatdb l;
#include "matvec.h"
// that is it above

typedef vecdbl memvec;

struct felt
{
..
memvec m; // <- incomplete type error

I don't understand Vec():Vec<T>() and probably you're compiler
doesn't either eventually makeing Vec<Tincomplete classes
because of that problem.
Also on a different aspect: std::vector was not written to be
derived from.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Sep 30 '06 #3
im*****@hotmail .co.uk wrote:
#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec():Vec<T>() { }
Vec() : std::vector<T>( ) {}
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
& operator[](int i) { return this->at(i); }
const T& operator[](int i ) const { return at(i); }
const T& operator[](int i ) const { return this->at(i); }
};
typedef Vec<doublevecdb l;
typedef Vec<vecdblmatdb l;
#include "matvec.h"
// that is it above

typedef vecdbl memvec;

struct felt
{
..
memvec m; *// <- incomplete type error

Best

Kai-Uwe Bux
Sep 30 '06 #4
I don't understand Vec():Vec<T>() and probably you're compiler
doesn't either eventually makeing Vec<Tincomplete classes
because of that problem.
I tried these but it gives same error

template < typename T >
class Vec : public std::vector< T {
public:
Vec(): std::vector<T>( ) { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};

and

template < typename T >
class Vec : public std::vector< T {
public:
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};

Do I need std:: infront of anything else ? What is the "(s)" about ?

Sep 30 '06 #5
Greg Comeau wrote:
In article <ef**********@p anix1.panix.com >,
Greg Comeau <co****@comeauc omputing.comwro te:
>>In article <11************ **********@c28g 2000cwb.googleg roups.com>,
<im*****@hotma il.co.ukwrote:
>>>Hi,
I am migrating some std::vectors to use a template instead, but I get
an incomplete type error in a struct declaration.

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec():Vec<T>() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return at(i); }
const T& operator[](int i ) const { return at(i); }
};
typedef Vec<doublevecdb l;
typedef Vec<vecdblmatdb l;
#include "matvec.h"
// that is it above

typedef vecdbl memvec;

struct felt
{
..
memvec m; // <- incomplete type error

I don't understand Vec():Vec<T>() and probably you're compiler
doesn't either eventually makeing Vec<Tincomplete classes
because of that problem.

Also on a different aspect: std::vector was not written to be
derived from.
If you look at the OP's code, the template class Vec<Tis clearly a
debugging tool: I would bet that the intended use is to redefine some
typedefs that currently go to std::vector to hunt down some out of bounds
errors. Once those are fixed, the typedef will hopefully be changed back to
std::vector in production code. I cannot see any harm in this little trick.

Of course, a full-fledged debugging vector (e.g., whose iterators would
guard against invalidation) would be even better.
Best

Kai-Uwe Bux
Sep 30 '06 #6
Greg Comeau wrote:
Also on a different aspect: std::vector was not written to be
derived from.
--
Why was I recommended to wrap this in a template in the first place ?

Sep 30 '06 #7
im*****@hotmail .co.uk wrote:
Greg Comeau wrote:
>Also on a different aspect: std::vector was not written to be
derived from.
--

Why was I recommended to wrap this in a template in the first place ?
To cut a long story short: std::vector does not have a virtual destructor.
That implies that you get undefined behavior if you use a std::vector<T>*
polymorphically and you delete an object of a derived class through this
pointer. Another source of surprises is that references to derived objects
will silently convert to references of type std::vector<T>.

Certain people think that it is therefore a BadIdea(tm) to ever inherit
publicly from std::vector<T>. Others think it's ok as long as you know
about the possible traps. In your case, it looks as though you use this
inheritance just as a temporary debugging tool and there is probably no
harm. Also, you could go back to std::vector<Tin production code once
the out of bounds errors have been found. In principle, you should be able
to do the switch with a simple typedef in your code.

Inheriting from std::vector<Tca n be tricky. That does not imply it is
always bad. But you have to be aware of the difficulties that can arise.
Best

Kai-Uwe Bux
Sep 30 '06 #8

Kai-Uwe Bux wrote:
Inheriting from std::vector<Tca n be tricky. That does not imply it is
always bad. But you have to be aware of the difficulties that can arise.
Best

Kai-Uwe Bux
Thanks, I'll check this out. Does this count as inheriting ?

class myc
{
std::vector<dou blex;
...

Sep 30 '06 #9
im*****@hotmail .co.uk wrote:
>
Kai-Uwe Bux wrote:
>Inheriting from std::vector<Tca n be tricky. That does not imply it is
always bad. But you have to be aware of the difficulties that can arise.
Best

Kai-Uwe Bux

Thanks, I'll check this out. Does this count as inheriting ?

class myc
{
std::vector<dou blex;
..
Nope, that is usually called composition. The drawback is that you have to
write tons of boilerplate code to forward all the member functions.

Also, I did not intend to scare you away from public inheritance in this
particular case. If you do not use pointers to std::vector polymorphically
(and you don't have a reason to do that anyhow), you will almost certainly
be fine.

To complete the picture, you might want to read up on private inheritance:

class my_vector : private std::vector<dou ble{

using iterator;
using ...
// tons of using declarations forwarding all members that we want.

};

This avoids the pitfalls of public inheritance and is less boilerplate code
than composition.
Best

Kai-Uwe Bux
Sep 30 '06 #10

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

Similar topics

5
33598
by: Lou Pecora | last post by:
g++ compiler error question. I have a container C whose constructor takes a class B that is inherited from an abstract class A. So I have the line of code: B binstance; C cinstance(binstance); The compiler gives the error,
3
2923
by: onsbomma | last post by:
Can anyone tell me the difference between typedef struct chunkinfo* mfastbinptr; and typedef struct chunkinfoptr chunkinfoptr;
2
40857
by: Halid Umar A M | last post by:
Dear All, Please tell me why this error is occuring. The following is the code snippets which i have typed. struct mystructure{ struct list_head m; //error: field m has incomplete type uint32_t ip; time_t time; };
2
3851
by: Alan | last post by:
I get a compilation error "parameter 2 of `Clusters sorted(Clusters, func_obj)' has incomplete type `func_obj' " when attempting to compile the following code: some_clusters = sorted(some_clusters,less_Eccentricity()); I`m trying to pass a function object (less_Eccentricity) into a higher level function that uses it in the call to sort.
5
5536
by: Belebele | last post by:
The code below does not compile under g++. There is an outer class template that contains and inner class template. The outer class template is fully specialized for int. Then a separate Foo class template inherits from the inner. The compiler indicates that the inner class is incomplete when used as the base class of the inner class template. Why? -------------------------------------------------------------------
2
3919
by: unclefester | last post by:
I have two classes: Test1 and Test2. Test1 has a field of data type Test2, & vice versa. I need some help in avoiding the incomplete type error ("Error: field has incomplete type"). Test1.h #ifndef _TEST1_ #define _TEST1_ #include "Test2.h" using namespace std;
5
3181
by: tejesh | last post by:
I am trying to compile the following code int backend_sm_run(struct interface_data *ctx) { xsup_assert((ctx != NULL), "ctx != NULL", TRUE); xsup_assert((ctx->statemachine != NULL), "ctx->statemachine != NULL", TRUE); backend_sm_check_globals(check);
5
9478
by: Anuz | last post by:
Hi all, While compiling a driver, I am getting this error: error: dereferencing pointer to incomplete type int __kc_adapter_clean(struct net_device *netdev, int *budget) { /*some initialization stuff */ struct adapter_struct *adapter = netdev_priv(netdev);
1
1603
by: byteit101 | last post by:
I have a big library i am making, and I have a Event<class t, class o> class, and a Controller class In the Controller.h I have this class Controller { //<this is line 22 in my file ... public: Event<float,Controller> XChanged; //Line 38 Event<float,Controller> YChanged; Event<float,Controller> ThrottleChanged;
0
8449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.