This question already has an answer here:
How to have an unordered_map where the value type is the class it's in?
(1 answer)
Closed 9 years ago.
I'm trying to do this in C++:
struct sagrup
{
int imps;
int clicks;
int uclicks;
int conversions;
int * variable;
unordered_map<int, struct sagrup> siguiente;
};
unordered_map<int, struct sagrup> agrupacion;
And I'm getting error: forward declaration of ‘struct sagrup’
I want to have that struct and add other struct into that ordered map so it will be like a tree.
Thanks to anyone that could help!
You have a couple issues:
First, you do not need to use struct everywhere. Second, are attempting to use an incomplete type with a template that requires a complete type definition (otherwise it doesn't know how to construct it). That map should be declared as a pointer, not another instance of sagrup.
The resulting code looks like this:
struct sagrup
{
int imps;
int clicks;
int uclicks;
int conversions;
int * variable;
unordered_map<int, sagrup*> siguiente;
};
unordered_map<int, sagrup> agrupacion;
Before the type has been fully defined, the compiler does not know anything about the type; i.e. what members are and their sizes and its in complete
Forward declaration is used only for references and pointers to such a struct.
Alternatively you can use:
unordered_map<int, sagrup*> siguiente;
inside your struct
Related
This question already has answers here:
Can an unnamed struct be made static?
(2 answers)
Passing anonymous structures to functions as parameter using C++ [duplicate]
(3 answers)
Closed 9 months ago.
I'm writing an app in C++ that interfaces with some code written in C.
The following is a simplified version of a struct defined in C code that I can't modify.
struct event{
uint8_t type;
union {
struct /* WishThisHadAName */ {
// ...
} connect;
struct {
// ...
} disconnect;
};
};
I'm trying to be able to define functions that take a pointer to the different unnamed structs, connect, disconnect, and all the others not listed above. To look something like the following.
void onConnect( struct WishThisHadAName *evt );
Is it possible to create this prototype without modifying the original C struct? Is there an easy way to create a wrapper for the unnamed structs to give them a name? Am I forced to create named structs that mirror the unnamed ones I'm trying to use?
I know you can get the type of an instantiated variable with decltype but I can't figure out how I could use that or declval in order to create the function prototype I'm looking for.
Simple as using event_connect_t = decltype(event::connect);.
Then you can use it as void onConnect( event_connect_t *evt );.
You can't declare a compatible type, but you can just extract the existing type declaration from the definition. decltype can resolve static member references just fine.
If you're using a compiler such as gcc/g++, you can use typeof to create a typedef for the anonymous type:
typedef __typeof__(((struct event){0}).connect) conn_type;
typedef __typeof__(((struct event){0}).disconnect) disconn_type;
Or without compound literals:
struct event e;
typedef __typeof__(e.connect) t1;
typedef __typeof__(e.disconnect) t2;
This question already has answers here:
How to define a typedef struct containing pointers to itself?
(2 answers)
Closed 3 years ago.
I wish to use structures to create a datatype which contains other references of an array of the structure itself OR structures of another type. Using templates will obviously result in an infinite declaration of the type itself when the reference is of same generic type. My current solution is to explicitly declare another struct type and the struct type of itself. I was wondering if there was a more clever solution (using structs).
struct Transform
{
float x;
float y;
float z;
float qx;
float qy;
float qz;
float qw;
};
struct Pattern
{
enum type{ linear, rectangular, circular };
char name[20];
int id;
Transform t[PATTERN_SIZE];
};
struct Group
{
enum type{ pattern, group };
char name[20;
int id;
Group *g[20];
Pattern *p[20];
};
It is really C-ish, but as long as you only need pointers, void * is a multi-purpose pointer:
struct Group
{
enum type{ pattern, group } t;
char name[20];
int id;
void *children[20]; // cast elements to `Group *` or `Pattern *` depending on t
};
If you want to have a variable that is either type A, or B (or C...), you can use std::variant type which was introduced in C++17, or its custom implementations if you compiler doesn't support it. This is how it can look.
For simple cases you can also do it manually using unions, although library solution is preferred. If you want specifically union/struct-only solution, I suggest you to direct your question to the C and not C++.
However, it looks like you are trying to create some sort of tree-like structure. For such structures in C++ I've found that simple inheritance works the best.
This question already has answers here:
Arduino: struct pointer as function parameter
(4 answers)
Closed 7 years ago.
I'm working on my Arduino project, which is the only C/C++ compiler I'm using.
I'm stumbling on why I cannot use the struct as a type on my function parameter.
Not sure if this specific to Arduino compiler design, or general C/C++ programming.
struct myStruct_t {
byte var1;
byte var2;
};
myStruct_t myStruct;
void setup() {
}
void loop() {
}
void myFunc(myStruct_t *myVar) {
int i = 0;
}
This results in a compiler errors:
error: variable or field 'myFunc' declared void
error: 'myStruct_t' was not declared in this scope
error: 'myVar' was not declared in this scope
If I comment out the declaration of the "myFunc" then it compiles as is.
I'm not clear on why I can declare and use a variable of that structure, however I can use the structure as a parameter type. Does the "struct" type not act as a type for parameter use?
Thanks.
Because in c you need to typedef to achieve that, otherwise you need to use struct to refer to the structure.
I'd recommend against typedefing and also against the _t in the structure name, but if you want it that way just do this
typedef struct myStruct_t {
byte var1;
byte var2;
} myStruct_t;
and do not use global variables, pass variables as parameters.
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 9 years ago.
I'm trying to initialize an array called ARRAY inside the constructor of a class called CLASS_A and this array is supposed to be accessible by the nested class CLASS_B. I thought of making the array a template but that was when things started to get a little fuzzy with how to go about the syntax.
I'm almost certain that declaring the ARRAY static is part of the solution and maybe making the type const int a template like A so then the template would look like template <class A, const int D> and the declaration A ARRAY[SIZE];. Anyways I initially got this error compiling the code below and then I made some changes and got a different error.
This is a linker error by the way
nested_class_incomplete_type.cpp|16|undefined reference to `CLASS_A<3>::ARRAY'|
nested_class_incomplete_type.cpp|28|undefined reference to `CLASS_A<3>::ARRAY'|
edit duplicate code.
You only declared ARRAY in CLASS_A, you need to define it:
template <const int D>
int CLASS_A<D>::ARRAY[SIZE] = {};
BTW, your SIZE field is redundant, you don't need it, e.g:
template <size_t N>
class CLASS_A
{
public:
static int ARRAY[N];
//.....
};
template <size_t N>
int CLASS_A<N>::ARRAY[N] = {};
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forward declarations of unnamed struct
If I have
typedef struct tagPAGERANGE
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I can forward declare it that way
struct tagPAGERANGE;
typedef struct tagPAGERANGE PAGERANGE;
But what I have is
typedef struct
{
int iFirstPage;
int iLastPage;
} PAGERANGE;
I'm not sure how I can do it. I only want to hold a pointer to this struct. Right now I'm stuck with either including a rather substantial header, or duplicating the definition of the struct.
It's impossible. You can only declare named structs.
Think about what identifies a struct that doesn't have a name, and how do you tell the compiler that it's that struct you want. If it doesn't have a name, it's identified by its members, so you need to provide members — i.e. define it. Therefore, you can't just declare it — you don't have a luxury of an identifier other than the definition itself.
Since this is used in a C++ code, just get rid of the typedefs altogether, they are unnecessary and bad style in C++.
The real solution is to just use named structs:
struct foo; // forward declaration
struct foo {
// … implementation
};
The typedefs are not useful.