[英]Shall I use the structs as a Parameterized Constructor in C++?
This is my sample code.这是我的示例代码。 I used it like this, but I had an error from //error-line .
我是这样使用它的,但是我在//error-line 中遇到了错误。
The following error appeared:出现以下错误:
Variable has incomplete type 'std::TestCpp:: TestString'
变量的类型不完整 'std::TestCpp::TestString'
class TestCpp{
public:
any value;
struct TestString;
TestCpp(TestString Value){ // error
this->value = static_cast<any>(Value);
cout << Value;
}
};
You are using an instance of TestString
in the constructor of TestCpp
before it has been defined.您在
TestCpp
的构造函数中使用TestString
的实例,然后再定义它。 You should first define TestString
, then define the constructor of TestCpp
.您应该首先定义
TestString
,然后定义TestCpp
的构造TestCpp
。 My guess is you don't want to define TestString
where you declared it.我的猜测是你不想在你声明它的地方定义
TestString
。 To achieve that, you can do this by defining the constructor of TestCpp
in a source file for instance.为此,您可以通过在源文件中定义
TestCpp
的构造函数来TestCpp
。
// header file
struct TestCpp
{
std::any _value;
struct TestString;
TestCpp(TestString); // declare here, define in source file
};
// source file
#include "header_file"
struct TestCpp::TestString { /* ... */ };
TestCpp::TestCpp(TestString) { /* ... */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.