声明用户定义类型的变量以供以后初始化 [英] Declaring a variable of user-defined type for later initialization
问题描述
我想创建一个名为 process
的全局变量,而无需在第一时刻分配任何东西。
可以在C#中完成,如下:
class class TestCS
{
//创建一个变量
private System.Diagnostics处理过程;
private void SomeMethod()
{
//为其分配一个新生成的进程
process = Process.Start(file.exe, - );
process.WaitForInputIdle();
}
}
代码下面用C ++完成同样的事情。 过程
变量的类型为子
(从 Boost :: Process v0.31)。
b
class Test
{
public:
void SomeFunction();
private:
std :: string testString; //声明一个测试字符串
static const std :: string program_name;
static const std :: vector< std :: string> program_args;
boost :: process :: child process; //试图声明一个类型为'boost :: process :: child'的变量
};
Test.cpp
void Test :: SomeFunction()
{
testString =abc; //我可以在这行上成功定义测试变量
std :: cout<< testString;
boost :: process :: context ctxt;
//下面两个变量也一样
const std :: string program_name =startme.exe;
const std :: vector< std :: string> program_args = {/ test};
//我想在这里定义过程变量...
process = boost :: process :: launch(program_name,program_args,ctxt);
}
Main.cpp
$ b
int main()
{
Test test1;
test1.SomeFunction();
cin.get(); // pause
return 0;
}
但是,它会返回以下错误: Test.cpp :
错误C2512:'boost :: process :: child':没有适当的默认构造函数
如何正确完成?
<$ class =h2_lin>解决方案
由于错误状态,'boost :: process :: child'没有适当的默认构造函数
。这意味着 child
对象必须用一个接受参数的构造函数构造。
/ p>
class Foo
{
Foo //这是_default_构造函数。
Foo(int i); //这是另一个构造函数。
};
Foo f; //`Foo();`构造函数将使用_by default_。
如果我们将该类更改为以下内容:
class Foo
{
Foo(int i); //这是构造函数,没有默认构造函数声明。
};
Foo f; //这是无效的。
Foo f(1); //这很好,因为它满足`Foo(int i)的参数;
构建 string
的原因是它提供了一个默认的构造函数(这是一个空字符串),而 process :: child
类不。
因此,在构建时,您需要初始化 process :: child
对象。因为它是 TestClass
(而不是指针或引用)的一部分,所以当 TestClass
object 。
Test :: Test()
:process /在这里初始化成员,用任何args。
{
SomeFunction();
}
或...
class Test
{
//让它成为一个指针。
boost :: process :: child * pProcess;
};
Test :: Test()
{
pProcess = new boost :: process :: child(); //并在任何时候分配它。
SomeFunction();
}
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 the child
object must be constructed with a constructor that takes arguments.
Take the following example class
class Foo
{
Foo(); // This is the _default_ constructor.
Foo(int i); // This is another constructor.
};
Foo f; // The `Foo();` constructor will be used _by default_.
If we change that class to the following:
class Foo
{
Foo(int i); // This is the constructor, there is no default constructor declared.
};
Foo f; // This is invalid.
Foo f(1); // This is fine, as it satisfies arguments for `Foo(int i);
The reason your string
is constructed is because it provides a default constructor (which is an empty string), while the process::child
class does not.
Therefore, you need to initialize you process::child
object when it's constructed. Since it's a part of TestClass
(and not a pointer or a reference), it needs to be constructed when the TestClass
object is constructed.
Test::Test()
: process() // Initialize the member here, with whatever args.
{
SomeFunction();
}
Or...
class Test
{
// Make it a pointer.
boost::process::child* pProcess;
};
Test::Test()
{
pProcess = new boost::process::child(); // And allocate it whenever you want.
SomeFunction();
}
这篇关于声明用户定义类型的变量以供以后初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!