Unknown size of Foo in Bar.cpp!



 DEVELOP > c-Plus-Plus > Unknown size of Foo in Bar.cpp!

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jason Heyes"
Date: 30 Jun 2005 02:46:29 PM
Object: Unknown size of Foo in Bar.cpp!
Why does Foo.h need to be included in Bar.cpp for this program to compile
properly? I define the FooList destructor in a separate module so that a
forward declaration to Foo can be used.
// FooList.h
#include <vector>
class Foo;
class FooList
{
std::vector<Foo> v;
public:
~FooList(); // destructor interface
};
// FooList.cpp
#include "FooList.h"
#include "Foo.h" // destructor definition this
FooList::~FooList() { }// destructor definition
// Bar.cpp
#include "FooList.h"
int main()
{
FooList foos;
return 0;
}
Error Message
==========
Bar.cpp
c:\program files\microsoft visual studio\vc98\include\vector(59): error
C2036: 'Foo *': unknown size
c:\program files\microsoft visual studio\vc98\include\vector(58): while
compiling class-template member function 'vector<Foo>::~vector<Foo>(void)'
I don't want to include Foo.h in Bar.cpp unless I have to. Do I have to? If
so, why? Thanks a bunch.
.

User: "Victor Bazarov"

Title: Re: Unknown size of Foo in Bar.cpp! 30 Jun 2005 02:56:44 PM
Jason Heyes wrote:

Why does Foo.h need to be included in Bar.cpp for this program to compile
properly? I define the FooList destructor in a separate module so that a
forward declaration to Foo can be used.


// FooList.h

#include <vector>

class Foo;

class FooList
{
std::vector<Foo> v;

public:
~FooList(); // destructor interface
};


// FooList.cpp

#include "FooList.h"
#include "Foo.h" // destructor definition this

FooList::~FooList() { }// destructor definition


// Bar.cpp

#include "FooList.h"

int main()
{
FooList foos;

But you don't include the definition of 'Foo' here either. You have to!

return 0;
}


Error Message
==========

Bar.cpp
c:\program files\microsoft visual studio\vc98\include\vector(59): error
C2036: 'Foo *': unknown size
c:\program files\microsoft visual studio\vc98\include\vector(58): while
compiling class-template member function 'vector<Foo>::~vector<Foo>(void)'


I don't want to include Foo.h in Bar.cpp unless I have to. Do I have to? If
so, why? Thanks a bunch.

Yes, you have to. But you only need it in 'Bar.cpp', not in 'FooList.h'.
V
.
User: "Jason Heyes"

Title: Re: Unknown size of Foo in Bar.cpp! 30 Jun 2005 03:18:15 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:X7Ywe.11891$Tf5.3379@newsread1.mlpsca01.us.to.verio.net...


Yes, you have to. But you only need it in 'Bar.cpp', not in 'FooList.h'.

Weird. Bar doesn't look like it even uses Foo. All that happens in Bar.cpp
is a call to FooList::FooList and a call to FooList::~FooList, both of which
are defined in a separate module FooList.cpp. So aren't these just calls to
external functions?
.
User: "Victor Bazarov"

Title: Re: Unknown size of Foo in Bar.cpp! 30 Jun 2005 03:54:34 PM
Jason Heyes wrote:

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:X7Ywe.11891$Tf5.3379@newsread1.mlpsca01.us.to.verio.net...

Yes, you have to. But you only need it in 'Bar.cpp', not in 'FooList.h'.



Weird. Bar doesn't look like it even uses Foo. All that happens in Bar.cpp
is a call to FooList::FooList and a call to FooList::~FooList, both of which
are defined in a separate module FooList.cpp. So aren't these just calls to
external functions?

It has to allocate memory for 'FooList', doesn't it? How much to
allocate? So, it needs to instantiate 'std::vector<Foo>' for that,
don't it? So, to instantiate, it needs to know hat 'Foo' is, right?
V
.

User: "Donovan Rebbechi"

Title: Re: Unknown size of Foo in Bar.cpp! 30 Jun 2005 04:00:45 PM
On 2005-06-30, Jason Heyes <jasonheyes@optusnet.com.au> wrote:

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:X7Ywe.11891$Tf5.3379@newsread1.mlpsca01.us.to.verio.net...


Yes, you have to. But you only need it in 'Bar.cpp', not in 'FooList.h'.


Weird. Bar doesn't look like it even uses Foo. All that happens in Bar.cpp
is a call to FooList::FooList and a call to FooList::~FooList, both of which
are defined in a separate module FooList.cpp. So aren't these just calls to
external functions?

The problem is that FooList::~FooList() isn't responsible for calling the
destructor function vector<Foo>::~vector(). The vector destructor gets called
after ~FooList() has completed it, so ~vector() needs to be instantiated in
the block of main() { }. If you used vector<Foo>* instead, then you'd be
able to "hide" the destruction inside ~FooList(), but that almost defeats the
purpose of using vector in the first place.
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER