如何解决此链接错误
我有一个类 A,它是一个抽象基类。(C++)。现在,我有两个类B和C,它们继承自A;
我在 A 中有一个虚拟析构函数; 类 A 中的构造函数是受保护的。 现在,在 B 和 C 的构造函数中,我包含了对 A 构造函数的调用。
B::B():A()
{
//do something
}
对于 C
C::C():A()
{
//do something
}
Now 来说也是如此,在编译时我遇到了链接错误。
B.obj : error LNK2019: unresolved external symbol "protected: __
thiscall A::A(void)" (??0A) referenced in function "protected: __thiscall B::B(void)" (??0B)
C.obj : error LNK2001: unresolved external symbol "protected:
__thiscall A::A(void)" (??0A@XZ)
Error.
请建议如何解决这个问题。
谢谢, 卡蒂克。
I have a class A, that is an abstract base class.(C++). Now, I have two classes B and C which inherit from A;
I have a virtual destructor in A;
The constructor in class A is protected.
Now, in the constructors of B and C, I have included a call to A's constructor.
B::B():A()
{
//do something
}
similarly for C
C::C():A()
{
//do something
}
Now, while compiling I'm getting linking errors.
B.obj : error LNK2019: unresolved external symbol "protected: __
thiscall A::A(void)" (??0A) referenced in function "protected: __thiscall B::B(void)" (??0B)
C.obj : error LNK2001: unresolved external symbol "protected:
__thiscall A::A(void)" (??0A@XZ)
Error.
Please suggest how to resolve this.
Thanks,
Karhtik.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,实际上没有必要显式“调用”基类构造函数。将自动为您调用基类的默认构造函数。
其次,正如 @DeadMG 已经指出的那样,您收到的错误表明您显式声明了
A::A()
构造函数,但忘记定义它。Firstly, there's really no need to "call" the base class constructor explicitly. The default constructor of the base class will be called for you automatically.
Secondly, as @DeadMG already noted, the error you are getting suggests that you explicitly declared the
A::A()
constructor, but forgot to define it.这表明您在声明 A 时从未定义过它的默认构造函数。
This suggests that you never defined the default constructor of A when you declared it.