Implementing functions outside of sub class causing unknown error - c++

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

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.

Related

Linker error : error LNK2019: unresolved external symbol

I'm new to C++ and have encountered a problem while running my app. I googled the problem but since most results were with linking libraries I started a new thread.
I have a class CResizableDialog which I'm inheriting from my VtkDialogTest2 dialog class.
VtkDialogTest2.h;
#pragma once
#include "CResizableDialog.h"
#ifdef _WIN32_WCE
#error "CDHtmlDialog is not supported for Windows CE."
#endif
// VtkDialogTest2 dialog
class VtkDialogTest2 : public CResizableDialog
{
DECLARE_DYNCREATE(VtkDialogTest2)
public:
VtkDialogTest2(CWnd* pParent = NULL); // standard constructor
virtual ~VtkDialogTest2();
// Overrides
HRESULT OnButtonOK(IHTMLElement *pElement);
HRESULT OnButtonCancel(IHTMLElement *pElement);
// Dialog Data
enum { IDD = IDD_DIALOG4 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
VtkDialogTest2.cpp
#include "stdafx.h"
#include "Geometry.h"
#include "VtkDialogTest2.h"
IMPLEMENT_DYNCREATE(VtkDialogTest2, CResizableDialog)
VtkDialogTest2::VtkDialogTest2(CWnd* pParent /*=NULL*/)
: CResizableDialog(VtkDialogTest2::IDD, pParent),
{
}
VtkDialogTest2::~VtkDialogTest2()
{
}
void VtkDialogTest2::DoDataExchange(CDataExchange* pDX)
{
CResizableDialog::DoDataExchange(pDX);
}
BOOL VtkDialogTest2::OnInitDialog()
{
CResizableDialog::OnInitDialog();
//some code
return TRUE; // return TRUE unless you set the focus to a control
}
BEGIN_MESSAGE_MAP(VtkDialogTest2, CResizableDialog)
ON_BN_CLICKED(IDOK, &VtkDialogTest2::OnBnClickedOk)
END_MESSAGE_MAP()
//some code
I can't figure out what I'm doing wrong. I downloaded an example from the web which uses the CResizableDialog.h class the exact same way and copied both CResizableDialog.h and CResizableDialog.cpp into my project.
The errors I'm getting are;
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "public: __thiscall CResizableDialog::CResizableDialog(unsigned int,class CWnd *)" (??0CResizableDialog##QAE#IPAVCWnd###Z) referenced in function "public: __thiscall VtkDialogTest2::VtkDialogTest2(class CWnd *)" (??0VtkDialogTest2##QAE#PAVCWnd###Z)
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "protected: virtual int __thiscall CResizableDialog::OnInitDialog(void)" (?OnInitDialog#CResizableDialog##MAEHXZ) referenced in function "protected: virtual int __thiscall VtkDialogTest2::OnInitDialog(void)" (?OnInitDialog#VtkDialogTest2##MAEHXZ)
1>VtkDialogTest2.obj : error LNK2001: unresolved external symbol "protected: static struct AFX_MSGMAP const * __stdcall CResizableDialog::GetThisMessageMap(void)" (?GetThisMessageMap#CResizableDialog##KGPBUAFX_MSGMAP##XZ)
1>C:\Users\Geometry.exe : fatal error LNK1120: 3 unresolved externals
Any input will be highly appreciated.
The error was because I copied the CResizableDialog.h and CResizableDialog.cpp files directly into the project folder. I later noticed that they didn't show up in the solution window and copied them to the window as well. After that the errors disappeared.

Template Specialization with External Errors

When I attempted to specialize one of my template functions, Visual Studio threw me an external error, including an error for a function that was not specialized.
The three errors:
1>------ Build started: Project: Project3, Configuration: Debug Win32 ------
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::reverse(void)" (?reverse#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::print(void)" (?print#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::insertionSort(void)" (?insertionSort#?$linearList#VFriendToken####UAEXXZ)
Here is the relevant part of the code:
template<class T>
class arrayList : public linearList<T>
{
public:
//other methods
void reverse();
void print();
void insertionSort();
};
template<class T>
void arrayList<T>::reverse()
{
//method body
}
template<>
void arrayList<FriendToken>::insertionSort()
{
//method body
}
template<>
void arrayList<FriendToken>::print()
{
//method body
}
template<class T>
void arrayList<T>::insertionSort(){}
template<class T>
void arrayList<T>::print(){}
Your example shows specializations of the arrayList member functions which I assume are supposed to be overriding their virtual equivalants in linearList. The linker is saying it cant find the virtual members in the class linearList which is not included in your example.
virtual void __thiscall linearList<class FriendToken>::reverse(void)
If I add a definition of linearList like this the linker is quiet (on MSVC2010, I also added a empty FriendToken class to make things work).
template<typename T>
class linearList
{
public:
virtual void reverse() = 0; //pure virtual
virtual void print() = 0;
virtual void insertionSort() = 0;
};
If this is not your problem please post the code for linearList and I will update my answer as that is surely the source of your problem.
If needed for reference here is how I used the function reverse to test:
arrayList<FriendToken> a;
static_cast<linearList<FriendToken>&>(a).reverse();

Compiler error LKN2019: Unresolved external symbol when dealing with inheritance

I have the following Files in C++:
Object.h:
class Object{
public:
Object(void);
~Object(void);
GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};
Player.h:
class Player : public Object{
public:
Player(void);
~Player(void);
GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};
Platform.h:
class Platform : public Object{
public:
Platform(void);
~Platform(void);
GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};
Pillar.h:
class Pillar : public Object{
public:
Pillar(void);
~Pillar(void);
GLfloat getLeft();
GLfloat getRight();
GLfloat getBottom();
GLfloat getTop();
};
Collision.h:
#include "Player.h"
#include "Platform.h"
#include "Pillar.h"
class Collision
{
public:
Collision(void);
~Collision(void);
bool CollisionDetect(Object* obj1, Object* obj2);
};
Collision.cpp:
bool Collision::CollisionDetect(Object* obj1, Object* obj2){
return !(obj2->getLeft() > obj1->getRight()
|| obj2->getRight() < obj1->getLeft()
|| obj2->getTop() > obj1->getBottom()
|| obj2->getBottom() < obj1->getTop());
}
CollisionDetect will be comparing whether 2 different Objects have collided and to do this I need specific x and y values however, the types of object may vary depending on different circumstances, and each type of object will have different ways to find their x and y values. This means that I have had to re-define the relevant get Methods in their subclasses, but when I run this code I end up with the errors:
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getLeft(void)" (?getLeft#Object##QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect#Collision##QAE_NPAVObject##0#Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getRight(void)" (?getRight#Object##QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect#Collision##QAE_NPAVObject##0#Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getBottom(void)" (?getBottom#Object##QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect#Collision##QAE_NPAVObject##0#Z)
1>Collision.obj : error LNK2019: unresolved external symbol "public: float __thiscall Object::getTop(void)" (?getTop#Object##QAEMXZ) referenced in function "public: bool __thiscall Collision::CollisionDetect(class Object *,class Object *)" (?CollisionDetect#Collision##QAE_NPAVObject##0#Z)
1>C:\Users\Student\Dropbox\Work\Year 2\Graphics 1\Coursework\GraphicsCoursework\Debug\GraphicsCoursework.exe : fatal error LNK1120: 4 unresolved externals
Is it a problem with my inheritance that is causing this, or is it something else?
Exactly make the 4 functions in the object class "virtual"
Infact i would suggest make it pure virtual,
class Object{
public:
Object(void);
~Object(void);
virtual GLfloat getLeft() = 0;
virtual GLfloat getRight() = 0;
virtual GLfloat getBottom() = 0;
virtual GLfloat getTop() = 0;
};
This will force you to override those four function in the derived class, and provide a definition..
Each of your classes, Derived as well as Base need to define those 4 member functions.
Only pure virtual functions in C++ are allowed to exist without a definition.
Also, the redeclaration and definition of these 4 member functions in derived classes HIDE the functions in the Base class. From your usage it seems you want to use dynamic polymorphism through function overidding and for that you need to use the keyword virtual.
seems like you need virtual functions.
class Object{
public:
Object(void);
~Object(void);
virtual GLfloat getLeft()=0;
virtual GLfloat getRight()=0;
virtual GLfloat getBottom()=0;
virtual GLfloat getTop()=0;
};

Linker errors creating QLabel with doubleclick event

I'm creating a QLabel subclass which adds the DoubleClickEvent to it. I have created the following, but I'm getting some strange linker errors, maybe someone can point out what I've done wrong?
//Header
#ifndef IMAGE_LABEL_H
#define IMAGE_LABEL_H
#include <QLabel>
#include <QMouseEvent>
class image_label : public QLabel
{
Q_OBJECT
public:
image_label(QWidget* parent = 0);
~image_label();
signals:
void doubleClicked();
protected:
void mouseDoubleClickEvent(QMouseEvent * e);
};
#endif
//CPP
#include "image_label.h"
#include <QMouseEvent>
image_label::image_label(QWidget* parent) : QLabel(parent)
{
}
image_label::~image_label()
{
}
void image_label::mouseDoubleClickEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
emit doubleClicked();
QLabel::mouseDoubleClickEvent(e);
}
}
I get the following linker errors when I compile:
image_label.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall image_label::metaObject(void)const " (?metaObject#image_label##UBEPBUQMetaObject##XZ)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall image_label::qt_metacast(char const *)" (?qt_metacast#image_label##UAEPAXPBD#Z)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall image_label::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#image_label##UAEHW4Call#QMetaObject##HPAPAX#Z)
image_label.obj : error LNK2019: unresolved external symbol "protected: void __thiscall image_label::doubleClicked(void)" (?doubleClicked#image_label##IAEXXZ) referenced in function "protected: virtual void __thiscall image_label::mouseDoubleClickEvent(class QMouseEvent *)" (?mouseDoubleClickEvent#image_label##MAEXPAVQMouseEvent###Z)
Can anybody help why I get these errors?
You must run the MOC preprocessor on the file image_label.h. This generates a file moc_image_label.cppthat you must include in the build. The error message indicates that you have not done this. (The symbols image_label::metaObject etc. that are mentioned in the error message are defined in moc_image_label.cpp.)

error LNK2019: unresolved external symbol

#include <iostream>
using namespace std;
class A {
public:
void function( int num);
bool function1()const;
virtual bool function2() const=0;
};
class B:public A {
public :
bool function2()const;
};
int _tmain(int argc, _TCHAR* argv[])
{
void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针
// Edit: Google translation of the above comment is
// "Not address, but a pointer to a member function pointer"
bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数
// Edit: Google translation of the above comment is
// "Point to a member function pointer can point to a const member function"
B b;
A *a=&b;
(a->*p1)();
(b.*p1)();
return 0;
}
but when I link it:
1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1#A##QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function#A##QAEXH#Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2#B##UBE_NXZ)
can you tell me why?
You haven't implemented A::function(), A::function1(), or B::function2(). You need to do that.
A::function1, A::function and B::function2 are all declared, but never defined. You can't get a pointer to the function if it is not defined, where would it point?