c++ - 在子类之外实现函数导致未知错误

标签 c++ oop inheritance subclass base-class

我正在尝试用 C++ 编写一个基本的图形用户界面库,但我在看似基本的继承方面遇到了问题。我在 Component.h 中声明了一个基类 Component

class Component
{
public:
    virtual void add(Component &c);
    virtual void remove(Component &c);
    virtual void setBounds(int x, int y, int width, int height);
    virtual void setLocation(int x, int y);
    virtual void setSize(int width, int height);
    virtual void setVisible(bool b);
};

我还在此处显示的同一 header 中声明了一个子类框架

class Frame : public Component
{
private:
    char* ftitle;
    HWND* hwnd;

public:
    Frame();
    Frame(char* title);
    void add(Component &c);
    void remove(Component &c);
    void setBounds(int x, int y, int width, int height);
    void setLocation(int x, int y);
    void setSize(int width, int height);
    void setVisible(bool b);
    void setTitle(char* title);
};

我在此处显示的另一个名为 Frame.cpp 的文件中实现了此类函数

#include "Component.h"

Frame::Frame()
{
    Frame("");
}

Frame::Frame(char* title)
{
    ftitle = title;
    *hwnd = CreateWindow("static", title, WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), NULL);
}

void Frame::setVisible(bool visible)
{
    if(visible)
    {
        ShowWindow(*hwnd, SW_SHOW);
    }
    else
    {
        ShowWindow(*hwnd, SW_HIDE);
    }
}

void Frame::add(Component &c){}
void Frame::remove(Component &c){}
void Frame::setBounds(int x, int y, int width, int height){}
void Frame::setLocation(int x, int y){}
void Frame::setSize(int width, int height){}
void Frame::setTitle(char* title){}

但是,当我尝试编译和构建项目时,出现如下所示的几个错误

1>------ Build started: Project: GUI, Configuration: Debug Win32 ------
1>  Frame.cpp
1>  Generating Code...
1>  Compiling...
1>  Main.cpp
1>  Generating Code...
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::add(class Component &)" (?add@Component@@UAEXAAV1@@Z)
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::remove(class Component &)" (?remove@Component@@UAEXAAV1@@Z)
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setBounds(int,int,int,int)" (?setBounds@Component@@UAEXHHHH@Z)
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setLocation(int,int)" (?setLocation@Component@@UAEXHH@Z)
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setSize(int,int)" (?setSize@Component@@UAEXHH@Z)
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setVisible(bool)" (?setVisible@Component@@UAEX_N@Z)
1>C:\Users\Owner\Documents\Visual Studio 2012\Projects\GUI\Debug\GUI.exe : fatal error LNK1120: 6 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

链接器提示您的 Component 类方法缺少实现,因为它们不是纯虚拟的。您可以通过将它们设为纯虚拟来解决此问题:

virtual void add(Component &c) = 0;

等等。

或者,提供实现。

请注意,您还应该为 Component 提供一个虚拟析构函数。

关于c++ - 在子类之外实现函数导致未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759015/

相关文章:

c++ - GTKmm 简单例子编译报错

c++ - 在构造函数初始化列表中初始化 stringstream 引用成员

objective-c - 如何将子类设置为 obj-c 中父类(super class)的属性?

Java 动态绑定(bind)和方法覆盖

javascript - 有没有有效的方法在 javascript 中使用 `__proto__` 或 `setPrototypeOf()`?

c++ - 插入数据时出现段错误

c++ - 将标志存储在指针中

在面向对象编程中传递 Python 参数

java - 检查有效的重载

Python-UnboundLocalError : local variable 'obj' referenced before assignment