I want to hand over to a function a pointer to an object and then work with this in the function. However this somehow does not work. Please excuse if my nomenclature is not correct. I haven't worked with c++ for a while. Here is my code:
#include <iostream>
#include <array>
#include <fstream>
class Grid {
public:
std::array<std::array<std::array<double,10>, 10>, 10> chi;
Grid();
};
Grid::Grid()
{
std::array<std::array<std::array<double,10>, 10>, 10> chi={};
}
void set_initial_conditions(Grid& g);
int main () {
Grid g();// greate object
set_initial_conditions(g);// set the initial conditions.
return 0;
}
void set_initial_conditions(Grid& g) {
std::cout<<"set initial conditions"<<std::endl;
}
When compiling with
c++ -o minimal_example minimal_example.cpp -std=c++17
I get the following error message:
minimal_example.cpp: In function ‘int main()’:
minimal_example.cpp:22:26: error: invalid initialization of reference of type ‘Grid&’ from expression of type ‘Grid()’
22 | set_initial_conditions(g);
| ^
minimal_example.cpp:18:35: note: in passing argument 1 of ‘void set_initial_conditions(Grid&)’
18 | void set_initial_conditions(Grid& g);
| ~~~~~~^