抽象构造函数中的抽象方法

发布于 2025-01-11 22:33:49 字数 934 浏览 3 评论 0原文

我想知道为什么我不能创建这样的东西? :

class Abstract_object
{
protected:
    virtual void _initialize() = 0;

public:
    Abstract_object()
    {
        _initialize();
    }
};

class Object : public Abstract_object
{
protected:
    void _initialize()
    {
         std::cout << "Initate client !" << std::endl;
    }

public:
    Object () : Abstract_object()
    {
        
    }
};

int main()
{
    Object* tmp = new Object();

    return (0);
}

当我尝试编译它时,我收到了一个未解析的外部符号,我完全不明白它来自哪里:_initialize 的定义确实在孩子中找到......有人可以向我解释发生了什么? =)

我尝试在子类中调用 _initiate ,它确实按预期工作。但我将不得不创建多个类来增强抽象类,并且我不想在每个类中调用 _initiate ,所以我正在寻找一个转机,如果有人有的话 =)

谢谢你们! =)

这是我编译时遇到的错误: 错误 LNK2019 无法解析的外部符号“protected: virtual void __thiscall Abstract_object::_initialize(void)” (?_initialize@Abstract_object@@MAEXXZ) 在函数“public: __thiscall Abstract_object::Abstract_object(void)” (??0Abstract_object@@QAE) 中引用@XZ)

I was wondering why can't i create something like this ? :

class Abstract_object
{
protected:
    virtual void _initialize() = 0;

public:
    Abstract_object()
    {
        _initialize();
    }
};

class Object : public Abstract_object
{
protected:
    void _initialize()
    {
         std::cout << "Initate client !" << std::endl;
    }

public:
    Object () : Abstract_object()
    {
        
    }
};

int main()
{
    Object* tmp = new Object();

    return (0);
}

When i try to compile it, i recieved a unresolved external symbol, which i quite don't understand where it came from : the definition of _initialize is indeed found in the children ... Someone can explain to me what is happening ? =)

i have tried to call _initiate inside the children class, and it did work as intended. But i will have to create multiple class enhancing Abstract class, and i do not want to call _initiate inside each one of them, so i'm looking for a turnaround, if someone have it =)

Thank you guys ! =)

This is the error i got when compiling this :
Error LNK2019 unresolved external symbol "protected: virtual void __thiscall Abstract_object::_initialize(void)" (?_initialize@Abstract_object@@MAEXXZ) referenced in function "public: __thiscall Abstract_object::Abstract_object(void)" (??0Abstract_object@@QAE@XZ)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2025-01-18 22:33:49

在构造函数中使用了函数 _initialize

Abstract_object()
{
    _initialize();
}

虽然它是一个纯虚函数,但是您需要为类 Abstract_object 定义它。例如在类定义之后写

void Abstract_object::_initialize()
{
    //...some code
}

In the constructor there is used the function _initialize

Abstract_object()
{
    _initialize();
}

Though it is a pure virtual function nevertheless you need to define it for the class Abstract_object. For example write after the class definition

void Abstract_object::_initialize()
{
    //...some code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文