473,324 Members | 2,456 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,324 software developers and data experts.

forward declaration of Pointer within an array of struct

i have the following 2 structs and i have an vector array for storing each type.


Expand|Select|Wrap|Line Numbers
  1. struct hashEntry{
  2.     int element;
  3.     EntryType info;
  4.     struct heapEntry *ptr;
  5.     hashEntry( ) {}
  6.     hashEntry(  int i, EntryType t = EMPTY ) : element(i), info(t){}
  7. };
  8.  
  9.  
  10. struct heapEntry {
  11.         int element;
  12.         struct hashEntry *ptr;
  13.     } ;
  14.  
  15. //this array is in class binaryHeap
  16.  vector<heapEntry> heapArray;
  17.  
  18. //this array is in class hashTable
  19.  vector<hashEntry> hashArray;
  20.  
  21. void Quash::hashTable::link()
  22. {
  23.     cout<< (*hashArray[0].ptr).element;
  24. }
  25.  
  26.  
My problem occurs when calling link() i get the following error.

Quash.cpp: In member function void Quash::hashTable::link():
Quash.cpp:153: error: invalid use of undefined type struct heapEntry
Quash.h:17: error: forward declaration of struct heapEntry


line 17 is the line:
struct heapEntry *ptr;

line 153 is the line:
cout<< (*hashArray[0].ptr).element;


Any hints?
Nov 7 '07 #1
1 3585
weaknessforcats
9,208 Expert Mod 8TB
hashEntry{
int element;
EntryType info;
struct heapEntry *ptr; <<<<<<<<
hashEntry( ) {}
hashEntry( int i, EntryType t = EMPTY ) : element(i), info(t){}
};
You can't use heapEntry* unless a) the compiler has already seen the struct or b) you use a forward reference:
Expand|Select|Wrap|Line Numbers
  1. struct hashEntry;    //forward reference
  2.  
  3. hashEntry{
  4.     int element;
  5.     EntryType info;
  6.     struct heapEntry *ptr;
  7.     hashEntry( ) {}
  8.     hashEntry(  int i, EntryType t = EMPTY ) : element(i), info(t){}
  9. };
  10.  
A forward reference just tells the compiler that the struct exists. That's enough information to allow the compiler to accept a pointer to the struct.

cout<< (*hashArray[0].ptr).element;
This line is incorrect. You have vector of hashEntry and the hashEntry has a pointer to the heapEntry. To access the heapEntry referencec by the pointer you code:
Expand|Select|Wrap|Line Numbers
  1. cout<< *(hashArray[0].ptr).element;
  2.  
That is, you de-reference the member of the struct. The better style is to use:
Expand|Select|Wrap|Line Numbers
  1. cout<< hashArray[0].ptr->element;
  2.  
It's easier to read.
Nov 7 '07 #2

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

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
5
by: Walt Karas | last post by:
Is this code legal under the standard? struct A { int i; struct C { unsigned offset_of_c(void) { return((unsigned) &(((A *) 0)->c)); }
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
1
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
11
by: Martin Eisenberg | last post by:
Hi Antoine, just redirecting you... Antoine Trux wrote: > Hi, > > Is the following code legal: > > ------> code starts here <------ > #include <stddef.h>
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
7
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.