As per Arduino Playground - Struct Resource
to use a struct as a function returntype, I need to put the struct in a header file.
However,
I don't have any issues with the following:
typedef struct VectorInt {
int x;
int y;
int z;
};
void setup() {
VectorInt test;
test.x = 3;
}
but when I move the struct to a .h in a new tab in the same folder as the .ino file, and #include that header, I get
'VectorInt' was not declared in this scope
What am I doing wrong?
Any help is appreciated, thank you.
system
2
What am I doing wrong?
The typedef statement creates a new type name for an existing type. The existing type is struct VectorInt. What is the new type name?
struct vectorInt
{
// stuff
};
typedef struct vectorInt VectorInt;
Now, the existing name is struct vectorInt and the new name is VectorInt.
LarryD
3
Put this in your header file. Then in your sketch #include "header.h"
struct _VectorInt {
int x;
int y;
int z;
};
typedef struct _VectorInt VectorInt;
Edit: Went to coffee PaulS posted
PaulS:
The typedef statement creates a new type name for an existing type. The existing type is struct VectorInt. What is the new type name?
struct vectorInt
{
// stuff
};
typedef struct vectorInt VectorInt;
Now, the existing name is struct vectorInt and the new name is VectorInt.
Did that, now have
struct _VectorInt {
int x;
int y;
int z;
};
typdef struct _VectorInt VectorInt;
in a header, I have #include for that header, now have
void setup() {
VectorInt test;
test.x = 3;
}
Still get 'VectorInt' was not declared in this scope
LarryD
5
Put VectorInt test; I front of setup()
show us your complete sketch with all lines of code.
LarryD
6
LarryD:
Put VectorInt test; I front of setup()
show us your complete sketch with all lines of code.
Header file:
struct _VectorInt {
int x;
int y;
int z;
};
typdef struct _VectorInt VectorInt;
#include <header.h>
void setup() {
VectorInt test;
test.x = 3;
}
void loop() {
}
Are you saying I can only use structs as global, not within functions?
Is there a way I can use a globally-typedef'd struct within a function?
edit: have also tried the following
#include <header.h>
VectorInt test = {1, 2, 3};
void setup() {
}
void loop() {
}
and get
error: 'VectorInt' does not name a type
[color=red]typdef[/color] struct _VectorInt VectorInt;
Also, you should use #include "header.h"
to include files in the same directory as the source file.
LarryD
9
typdef .... typedef
This works:
EDIT oops pasted the wrong code, corrected now
struct _VectorInt {
int x;
int y;
int z;
};
typdef struct _VectorInt VectorInt;
#include "header.h"
VectorInt test;
void setup() {
Serial.begin(9600);
test.x = 3;
Serial.println(test.x);
}
void loop() {
}
LarryD
10
Are you saying I can only use structs as global, not within functions?
Why do you need to do dynamic allocation?
Google "Arduino struct malloc".
LarryD
11
oops, pasted the wrong code in post #8, corrected now
Using typedef is a red herring. It's not needed in C++:
struct VectorInt {
int x;
int y;
int z;
};
// both of these work:
VectorInt a;
struct VectorInt b;
I think the real issue is that you don't #include the header file in the main sketch file (as oqibidipo mentioned).
LarryD:
Why do you need to do dynamic allocation?
Google "Arduino struct malloc".
I don't see where dynamic allocation was ever mentioned in this thread.