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?
As the error states,
'boost::process::child' no appropriate default constructor available
. This means that thechild
object must be constructed with a constructor that takes arguments.Take the following example class
If we change that class to the following:
The reason your
string
is constructed is because it provides a default constructor (which is an empty string), while theprocess::child
class does not.Therefore, you need to initialize you
process::child
object when it's constructed. Since it's a part ofTestClass
(and not a pointer or a reference), it needs to be constructed when theTestClass
object is constructed.Or...