Hi, I have a problem with a compiler error "invalid use of incomplete type". Here is the description:
in "first.h", I have: - class cache_t {
-
public:
-
virtual void printStats( pseq_t *pseq );
-
...
-
};
-
-
class generic_cache_block_t {
-
...
-
};
in "first.C", I have: - template class generic_cache_template<generic_cache_block_t>;
-
...
-
void cache_t::printStats( pseq_t *pseq )
-
{
-
....
-
}
in "second.H", I have: - generic_cache_template<generic_cache_block_t> *l2_cache;
-
....
and in "second.C", I have: - 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.
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.
Sorry, At the end of "first.h" right after the generic_cache_block_t class, I have: - template <class BlockType>
-
class generic_cache_template : public cache_t {
-
public:
-
....
-
protected:
-
...
-
};
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?
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 -
template <class BlockType>
-
class generic_cache_template : public cache_t {
-
....
-
};
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.
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: - /*------------------------------------------------------------------------*/
-
/* Forward class declaration(s) */
-
/*------------------------------------------------------------------------*/
-
class cache_t;
-
class generic_cache_block_t;
-
template <class BlockType> class generic_cache_template;
-
...
Now in the body of "first.C" I have (in order): - #include "third.h"
-
#include "first.h"
-
...
In the body of "second.C" I have (in order): - #include "third.h"
-
#include "second.h"
-
...
Sorry if I forgot lots of things...
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.
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. :)
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ...
|
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...
|
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,...
|
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...
|
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,...
|
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();
}
};
|
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...
|
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 =...
|
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
......
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |