I want to create a global variable called process
without assigning anything to it in a first moment. Later on I'll spawn a new process in the operating system, and assign it to that variable.
It can be done in C# like so:
class TestCS
{
// creating a variable
private System.Diagnostics.Process process;
private void SomeMethod()
{
// assigning a newly spawned process to it
process = Process.Start("file.exe", "-argument");
process.WaitForInputIdle();
}
}
I wrote the code below to accomplish the same thing with C++. The process
variable is of type child
(from Boost::Process v0.31). #includes are omitted for simplicity.
Test.hpp
class Test
{
public:
void SomeFunction();
private:
std::string testString; // declaring a test string
static const std::string program_name;
static const std::vector<std::string> program_args;
boost::process::child process; // attempting to declare a variable of type 'boost::process::child'
};
Test.cpp
void Test::SomeFunction()
{
testString = "abc"; // I can successfully define the test variable on this line
std::cout << testString;
boost::process::context ctxt;
// the same goes for the next two variables
const std::string program_name = "startme.exe";
const std::vector<std::string> program_args = {"/test"};
// and I want to define the process variable here as well...
process = boost::process::launch(program_name, program_args, ctxt);
}
Main.cpp
int main()
{
Test test1;
test1.SomeFunction();
cin.get(); // pause
return 0;
}
However, it returns the following error for Test.cpp:
error C2512: 'boost::process::child' : no appropriate default constructor available
How can it be done correctly?