繁体   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