'How can I declare a structure in C++ without defining it?
I have been following a tutorial at cplusplus.com/doc/tutorial. Near the end of the Functions page, it discussed prototyping a function. Later on, I read about structures. I thought it looked a little messy, and I like my code blocks after my main method. So I wondered if I could prototype a data structure. I tried this:
#include <iostream>
using namespace std;
struct aStruct;
int main() {
aStruct structure;
structure.a = 1;
structure.b = 2;
cout << structure.a << ", " << structure.b << "\n";
return 0;
}
struct aStruct {
int a;
int b;
};
But my IDE said that "aStruct was not declared in this scope". So, how would I prototype a data structure so the bulk of it can be below the main method?
The tutorial pages: Functions Data strucutres
Solution 1:[1]
Generally the only reason for pre-declaring a structure is for avoiding circular references in defining structures. The right solution is to use a header, and hide the structure definition in a header instead:
Inside aStruct.h
:
#ifndef _ASTRUCT_H
#define _ASTRUCT_H
struct aStruct {
int a;
int b;
};
#endif /* _ASTRUCT_H */
(the #ifdefs
/etc are to protection against accidental multiple-inclusion, which would otherwise cause an error)
Inside main.cpp
:
#include <iostream>
#include "aStruct.h"
using namespace std;
int main() {
aStruct structure;
structure.a = 1;
structure.b = 2;
cout << structure.a << ", " << structure.b << "\n";
return 0;
}
In the long run you will be much happier by organizing your code properly into separate files.
I will leave it as an exercise to the reader to properly protect against multiple inclusion of a header by using #ifdef ASTRUCT_H/#define ASTRUCT_H/#endif lines
.
Solution 2:[2]
How can I declare a structure in C++ without defining it?
In the exact way that you did in your code.
But my IDE said that "aStruct was not declared in this scope"
Well, that's a very confusing diagnostic. gcc says "error: aggregate 'aStruct structure' has incomplete type and cannot be defined". You cannot define an object of an incomplete type.
So, how would I prototype a data structure so the bulk of it can be below the main method?
You can't. If those 4 lines are too much, put the definition into a header. If you had any, you could leave the implementation of the member functions after main
or even in another compilation unit. Just like you can do with free functions.
Solution 3:[3]
aStruct
is an incomplete type, so the compiler does not know what a
and b
are.
This kind of trick, of forward declaring types can only be used when you need to specify the type as a class member or function parameter. However, it only works with pointers to the forwarded, incomplete type. The reason is, without the definition, the compiler does not know the size of the type, but a pointer always has the same size.
So, here is an example.
aStruct.h
struct aStruct
{
int a;
int b;
};
header.h
struct aStruct;
void func(aStruct* ptr);
source.cpp
#include "header.h" // at this point the compiler only knows of a type aStruct
#include "aStruct.h" // now the compiler has the definition of aStruct
void func(aStruct* ptr)
{
ptr->a = 42;
}
Solution 4:[4]
you can use forward declaration like
aStruct *structure;
1 #include <iostream>
2 using namespace std;
3
4 class aStruct;
5
6 int main() {
7
8 aStruct *structure ;
9 /*
10 structure->a = 1;
11 structure->b = 2;
12
13 cout << structure->a << ", " << structure->b << "\n";
14 */
15 return 0;
16 }
17
18 struct aStruct {
19 int a;
20 int b;
21 };
Solution 5:[5]
Regarding
” I wondered if I could prototype a data structure. I tried this:
#include <iostream> using namespace std; struct aStruct; int main() { aStruct structure; structure.a = 1; structure.b = 2;
No, there's no way to “prototype” a structure so its members can be used before the structure is defined.
You can put the structure definition inside main
or before main
, but at any rate it must come before the code that uses the members.
If you don't want to see it in your editor then you can
use an editor that can collapse regions of code, and/or
put the definition in a header file.
C and C++ are designed for nearly single-pass compilation, where generally every name that's used, must have already been declared. And that includes the fields of a structure. As far as I recall the only exception to this rule is jump labels, because without an exception for them some way of forward-declaring a label would be needed (more complexity) or forward jumps would have to be unsupported (undesirable restriction).
You can forward-declare a structure just like you're doing in the code above, but then you have the restriction that you discovered: that although the structure name itself is known and can be used (in limited ways), without a more complete definition there's really not much you can do with it. The next level up in completeness is to have a class definition, which specifies the members of the structure, and hence its size. It's complete enough to permit all use of the structure, but to make the code link you also need definitions of all member functions, if such have been declared but not defined inline in the class definition – and this is a third level of completeness, a fully complete definition.
With respect to standard terminology the forward declared structure is called an incomplete type, and the structure with a definition such that its size is known, is a complete type. There is no special name for the fully complete structure definition, with all member function definitions.
Solution 6:[6]
Place your struct definition above int main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | eerorika |
Solution 3 | Marius Bancila |
Solution 4 | resultsway |
Solution 5 | Cheers and hth. - Alf |
Solution 6 | Brian W |