![](/img/trans.png)
[英]c++ is confused whether to use parameterized constructor or copy constructor
[英]Shall I use the structs as a Parameterized Constructor in C++?
這是我的示例代碼。 我是這樣使用它的,但是我在//error-line 中遇到了錯誤。
出現以下錯誤:
變量的類型不完整 'std::TestCpp::TestString'
class TestCpp{
public:
any value;
struct TestString;
TestCpp(TestString Value){ // error
this->value = static_cast<any>(Value);
cout << Value;
}
};
您在TestCpp
的構造函數中使用TestString
的實例,然后再定義它。 您應該首先定義TestString
,然后定義TestCpp
的構造TestCpp
。 我的猜測是你不想在你聲明它的地方定義TestString
。 為此,您可以通過在源文件中定義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.