Reputation: 899
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 ==========
Upvotes: 0
Views: 126
Reputation: 227418
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.
Upvotes: 3