I’ve got something like:
typedef struct Data_s {
int field1;
int field2;
} Data;
class Foo {
void getData(Data& data);
void useData(Data& data);
}
In another class’s function, I might do:
class Bar {
Data data_;
void Bar::taskA() {
Foo.getData(data_);
Foo.useData(data_);
}
}
Is there a way to move the Data out of the global scope and into Foo without creating a new class? Data mirrors a struct existing in a library I’m using elsewhere. (i.e. same fields, just different name. I cast Data into the other struct later. I do this because Foo is an abstract class, and the derived class using the library is just one of many.)
Currently, just pasting it inside the class and replacing Data with Foo::Data everywhere doesn’t work.
class Foo {
typedef struct Data_s {
int field1;
int field2;
} Data;
...
}
I get 'Data' in class 'Foo' does not name a type at Bar data_;
You can define the struct inside the class, but you need to do it ahead of the place where you first use it. Structs, like classes themselves, must be forward-declared in order to use them:
Edit: Updated example to use the same fields/class names as listed in the original question. Please note that in order to be visible outside of the
Fooclass,Dataneeds to be declaredpublicand other code needs to reference it asFoo::Data.