473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 26186
Banfa
9,065 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,065 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,065 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,065 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

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

Similar topics

5
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 ...
22
by: Trevor Orton | last post by:
Hello, I'm having a slight problem using the W3C html validator and I've reviewed the FAQ's with no luck so hopefully someone here would be kind enough to point me in the right direction. I...
6
by: Pushkar Pradhan | last post by:
I tried to read the archives and solve this problem, but now I think I better post my problem: int main() { int blkSz = { {2,2}, {2,3}, ....., {6,6} }; write_bc_perf(mflops1, blkSz,...
5
by: kj | last post by:
Hi. I'm trying to compile some software from source, and I'm getting an error I can't figure out. The error in question is key_events.h:38: field `id' has incomplete type and the lines...
2
by: Anthony Borla | last post by:
Greetings, I hope everyone is enjoying the Holiday Season :) ! I'm attempting to implement a function template modelled somewhat on the STL's, 'generate' and 'generate_n' algorithms. Now,...
0
by: Arne Adams | last post by:
Hi, the following yields an internal compiler error with VC7.1 struct Nix { template<class T> operator T()const { return T(); } };
2
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...
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
1
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 ......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
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,...

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.