抽象构造函数中的抽象方法
我想知道为什么我不能创建这样的东西? :
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在构造函数中使用了函数 _initialize
虽然它是一个纯虚函数,但是您需要为类
Abstract_object
定义它。例如在类定义之后写In the constructor there is used the function _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