c++ - How to pass a dynamic array of structs to a member function? -
i wondering how can pass dynamically allocated array of structures main function member function of class. don't need change values in member function, print it.
if integer array in following code snippet, mystruct
not defined in myclass.h
, myclass.cpp
files. (which makes sense)
if include main.cpp
in myclass.h
lot of weird errors. idea prepending struct
in member function parameter, lead other errors well.
i need declare struct array outside of class, not member, , cannot use stl containers.
main.cpp:
#include "myclass.h" int main() { myclass my_class; struct mystruct { int a; int b; }; mystruct* struct_array = new mystruct[4]; // fill struct array values... my_class.printstructarray(struct_array); }
myclass.h:
#include <iostream> class myclass { // ... void printstructarray(mystruct* struct_array); };
myclass.cpp:
#include "myclass.h" void myclass::printstructarray(mystruct* struct_array) { std::cout << struct_array[0].a << struct_array[0].b; // ... }
just move struct
definition myclass.h
or it's own separate header file:
myclass.h
#include <iostream> struct mystruct { int a, b; }; class myclass { // ... void printstructarray(mystruct* struct_array); };
Comments
Post a Comment