0

I am trying to write a basic gui library in c++ and i am having problems with seemingly basic inheritence. I have a base class Component here delcared in Component.h

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);
};

I also have a subclass frame declared in the same header shown here

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);
};

And I implement this classes functions in another file named Frame.cpp shown here

#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){}

However when I try to compile and build the project i get several errors shown like this

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 ==========

1 Answer 1

3

The linker is complaining about a lack of implementations for your Component class methods, because they are not pure virtual. You can fix this by making them pure virtual:

virtual void add(Component &c) = 0;

and so on.

Or alternatively, provide implementations.

Note that you should also give Component a virtual destructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.