469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

error: invalid use of incomplete type

Hi, I have a problem with a compiler error "invalid use of incomplete type". Here is the description:
in "first.h", I have:
Expand|Select|Wrap|Line Numbers
  1. class cache_t {
  2. public:
  3.       virtual void printStats( pseq_t *pseq );
  4. ...
  5. };
  6.  
  7. class generic_cache_block_t {
  8. ...
  9. };
in "first.C", I have:
Expand|Select|Wrap|Line Numbers
  1. template class generic_cache_template<generic_cache_block_t>;
  2. ...
  3. void cache_t::printStats( pseq_t *pseq )
  4. {
  5.     ....
  6. }

in "second.H", I have:
Expand|Select|Wrap|Line Numbers
  1. generic_cache_template<generic_cache_block_t> *l2_cache;
  2. ....
and in "second.C", I have:
Expand|Select|Wrap|Line Numbers
  1. l2_cache->printStats( this );    //error
at the last line I get an error that
error: invalid use of incomplete type ‘struct generic_cache_template<generic_cache_block_t>’

I have searched about this error and some say it is occurred because of something called forward deceleration (a function is called but the
class has not been instantiated yet). I doubt about existence of such issue. Any idea is appreciated.
Mar 9 '10 #1
8 25060
Banfa
9,064 Expert Mod 8TB
I see no definition of generic_cache_template<> in your code, only a forward declaration line 1 of first.C

A forward declaration has this form

class <ClassName>;

It tells the compiler that a class with this name exists and may be defined at a later time. From this information the compiler can create pointers and references to the class but the compiler can not use any members of the class because they have not yet been declared in a class definition.

I think you have failed to post some of the relevent code.
Mar 9 '10 #2
Sorry, At the end of "first.h" right after the generic_cache_block_t class, I have:
Expand|Select|Wrap|Line Numbers
  1. template <class BlockType>
  2. class generic_cache_template : public cache_t {
  3. public:
  4.     ....
  5. protected:
  6.     ...
  7. };
I see no "class <ClassName>;" definition. So as you said, this is the source of error. "generic_cache_template" is dependent on "cache_t" but "cache_t" is not instantiated yet. Am I right?
Mar 9 '10 #3
Banfa
9,064 Expert Mod 8TB
No, this is not about instantiation but about definition of the types. Instantiation creates an object of a type but this is about the use of the types and how the types are defined and declared. If

Expand|Select|Wrap|Line Numbers
  1. template <class BlockType>
  2. class generic_cache_template : public cache_t {
  3.     ....
  4. };
Appears at the end of first.h then generic_cache_template is defined in the presence of the definition of cache_t, which it would have to be.

The problem is in second.C so the question is what does second.C #include and in what order and possibly what do the #included files #include.
Mar 9 '10 #4
I have not written this code however I want to use it under gcc 4.4.1. After searching for definitions I found a third file (!) called "third.h" and it contains all of the forward decelerations:
Expand|Select|Wrap|Line Numbers
  1. /*------------------------------------------------------------------------*/
  2. /* Forward class declaration(s)                                           */
  3. /*------------------------------------------------------------------------*/
  4. class cache_t;
  5. class generic_cache_block_t;
  6. template <class BlockType> class generic_cache_template;
  7. ...
Now in the body of "first.C" I have (in order):
Expand|Select|Wrap|Line Numbers
  1. #include "third.h"
  2. #include "first.h"
  3. ...
In the body of "second.C" I have (in order):
Expand|Select|Wrap|Line Numbers
  1. #include "third.h"
  2. #include "second.h"
  3. ...
Sorry if I forgot lots of things...
Mar 9 '10 #5
Banfa
9,064 Expert Mod 8TB
And that explains the problem, second.C is using generic_cache_template<generic_cache_block_t> through l2_cache but it only includes thrid.H and second.H.

thrid.H forward declares generic_cache_template<generic_cache_block_t> and second.H uses that forward declaration to declare the pointer l2_cache.

All well and good but second.C trys to access members of l2_cache requiring a full definition of generic_cache_template<generic_cache_block_t> but that definition is in first.H which is not included in second.C


Try include first.H into second.C (if you haven't already).

Don't worry too much but not including everything first time, when you don't quite understand why a problem it happening it is very hard to know what is and isn't relevant.
Mar 9 '10 #6
No that didn't help. I found that member functions for generic_cache_template were moved from first.C to first.h. I then put them back to first.C and now all things are well. I don't know why they move the definitions from .C to .h.
I still doubt if that was the real source of problem (maybe I mistakenly change something) because he error I got was related to forward declerations and we were redirected to that direction.

Thank you very much indeed for your helps and clue. Without your help I was not able to track the error. :)
Mar 10 '10 #7
At any cost. .h file should have only declaration, it should not have definition, In Second.h file, you trying to create the instance of the pointer object.
Mar 10 '10 #8
Banfa
9,064 Expert Mod 8TB
Not true for a template sridhard2406, all the definitions for a templated class or function need to be in the header file because it is a template for a class (or function), the compiler creates the actual class (or function) when it sees it in use. If a particular function in a templated class in not in the header then when that function gets used the template for the function is unavailable and the compiler can not create the actual function resulting in an unresolved external.
Mar 10 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Lou Pecora | last post: by
22 posts views Thread by Trevor Orton | last post: by
6 posts views Thread by Pushkar Pradhan | last post: by
5 posts views Thread by kj | last post: by
2 posts views Thread by Anthony Borla | last post: by
2 posts views Thread by Halid Umar A M | last post: by
1 post views Thread by byteit101 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.