我想创建一个名为的全局变量,process
而无需在第一时间为其分配任何内容。稍后我将在操作系统中生成一个新进程,并将其分配给该变量。
它可以像这样在 C# 中完成:
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();
}
}
我编写了下面的代码来用 C++ 完成同样的事情。该process
变量是类型child
(来自Boost::Process v0.31)。为简单起见,省略了#include 。
测试.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'
};
测试.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);
}
主文件
int main()
{
Test test1;
test1.SomeFunction();
cin.get(); // pause
return 0;
}
但是,它为Test.cpp返回以下错误:
错误 C2512:'boost::process::child':没有合适的默认构造函数可用
如何正确完成?