473,386 Members | 1,827 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,386 software developers and data experts.

invalid use of incomplete type struct...

I have this error.
"Invalid use of incomplete type struct Operand forward declaration of struct Operand". i got the same error some days ago, so i asked somewhere but the code was correct and i resolved creating again those files. But now i have again this error and i can't get rid of it :( I have this error in these 2 files.

barrel.h
Expand|Select|Wrap|Line Numbers
  1. #include "operand.h"
  2. #include <string>
  3. using std::string;
  4.  
  5. //class Operand;  Error in both cases.
  6. class Barrel: public Operand
  7. {
  8.         ...
  9.         virtual void empty();
  10.         virtual void fill();
  11.         virtual Barrel* mix(Operand* op2);
  12.  
  13.     protected:
  14.         double capacity, content, temperature;
  15. };
c.h
Expand|Select|Wrap|Line Numbers
  1.     #include <string>
  2.     #include <QtGui>
  3.     #include "barrel.h"
  4.     #include "calculatorwindow.h"
  5.     #include "operand.h"
  6.  
  7.  
  8.     using std::string;
  9.  
  10.     class CalculatorWindow;
  11.     class Barrel;
  12.     class Operand;
  13.  
  14.     class Calculator
  15.     {
  16.     public:
  17.         ...
  18.         Operand* operand1;
  19.         Operand* operand2;
  20.         double intoperand;
  21.         string operation;
  22.         string print;
  23.         bool continue;
  24.         ...
  25.         void Fill(CalculatorWindow * calc);
  26.         void Empty(CalculatorWindow * calc);
  27.         Operand* Mix(CalculatorWindow * calc);
  28.     };
  29.  
Oh, i forgot it.
operand.h
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using std::string;
  3.  
  4. class Operand
  5. {
  6. public:
  7.     virtual string print();
  8.     virtual Operand* Add(double a);
  9.     virtual void Subtract(double a);
  10.     virtual void empty();
  11.     virtual void fill();
  12.     virtual Operand* mix(Operand* op2);
  13. };
Apr 7 '12 #1
5 6690
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. //class Operand;  Error in both cases.
  2.  class Barrel: public Operand
  3.  {
  4.           ...
  5.           virtual void empty();
  6.           virtual void fill();
  7. etc...
requies that class Operand has an empty() and fill() method to override. The compiler can't tell if tis is true from a forward reference. You need to supply the full class definition of the Operand class.

A forward reference is only allowable when the compiler is asked to permit a pointer to a class object.
Apr 7 '12 #2
I have even tried to use operand as an abstract class, but nothing. So you say that i must completely define every method of operand?
Apr 7 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
When you use a virtual function you are supposed to use a base class pointer with a derived object. Like this:

Expand|Select|Wrap|Line Numbers
  1. Operand* ptr = new Barrel;
  2.  
  3. ptr->fill();
  4.  
The compiler cannot compile this code unless Operand has a fill() method. Thats's because ptr is an Operand*. There is no access to Barrel::fill() using this pointer.

There's no way to tell if there is a fill() method for Operand withut the full class declaration.

On the other hand, if you are doing this:

Expand|Select|Wrap|Line Numbers
  1. Barrel obj;
  2.        obj.fill();
  3.  
Then you are misusing polymotphism.

Those virtual functions in Barrel tell the compiler that if there is an Operand* variable being used for fill()and there is a choice between using Operand::fill() and Barrel::fill() that
Barrel::fill() is preferred.

Have a look at how a virtual function table (VTBL) works.
Apr 7 '12 #4
Well, i already have the SAME virtual methods in both operand and barrel (except for virtual barrel* mix() in barrel.h instead of virtual operand* mix() )but i have the same error.
Apr 8 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
One problem is here:
Expand|Select|Wrap|Line Numbers
  1. include <string>
  2.  2.     #include <QtGui>
  3.  3.     #include "barrel.h"
  4.  4.     #include "calculatorwindow.h"
  5.  5.     #include "operand.h"
  6.  
You barrel.h is before operand.h therefore when the compiler tries barrel.h it does not know about Operand yet.

Include operand.h before barrel.h.
Apr 8 '12 #6

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

Similar topics

7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
3
by: onsbomma | last post by:
Can anyone tell me the difference between typedef struct chunkinfo* mfastbinptr; and typedef struct chunkinfoptr chunkinfoptr;
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: 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...
7
by: Vivi | last post by:
Hello everybody, I'm writing a game program, and i have compilations errors.. :confused: typedef struct { struct lieu* cont; struct joueur* j;
3
by: John Sasso | last post by:
In my Yacc .y file I defined: %union { int value; struct Symbol Sym; } The Symbol struct I defined in a header file I #included in the Prologue section of the .y file as:
5
by: friend.05 | last post by:
1) #include <stdio.h> #include <stdlib.h> #include "graph.h" #define MAX_VTX 4 struct _graph_vertex {
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 =...
5
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...
8
by: mahmoodn | last post by:
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 );...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.