簡體   English   中英

我應該將結構用作 C++ 中的參數化構造函數嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM